PHP Operator with Example Code for Arithmetic, Comparison, Assignment, Increment, Decrement And Logical l and Conditional

PHP Operators

To perform the operations like-

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Increment/Decrement Operators
  • Logical Operators

on the variables PHP Operators are used.

PHP Arithmetic Operators

To perform the arithmetic operations like addition, subtraction, divide, multiplication etc “Arithmetic Operators” are used.

Operators Symbol Syntax Description
Addition + $x + $y To calculate the sum of the variables.
Subtraction $x – $y To calculate the difference between the variables.
Multiplication * $x * $y To calculate the product of the variables.
Division / $x / $y To calculate the division of the variables.
Modulus % $x % $y To calculate the remainder of the division.
Exponentiation ** $x ** $y To calculate the exponent(power) of the variable.

Example 1 – PHP Arithmetic Operators Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = 15;
		$y = 5;
		echo "The sum of variables ";
		echo $x + $y;
		echo "<br>";
		echo "The difference of variables ";
		echo $x - $y;
		echo "<br>";
		echo "The product of variables ";
		echo $x * $y;  
		echo "<br>";
		echo "The division of variables ";
		echo $x / $y;  
		echo "<br>";
		echo "The modulus(remainder) of variables ";
		echo $x % $y;  
		echo "<br>";
		echo "The exponent of variables ";
		echo $x ** $y;
	?>
  </body>
</html>

In the above examples, perform the arithmetic operations on the variables and shows the result of those operations on the web page.

Arithmetic Operator
Fig.1 – Arithmetic Operator.

PHP Comparison Operators

To shows, the comparison between two variables Comparison Operators is used.

Operators Symbol Syntax Description
Equal == $x == $y The result is true when both variables are equal.
Identical === ($x === $y) The result is true when both variables are equal and also have same data type.
Not equal != or <> $x != $y The result is true when both variables are not equal.
Not identical !== $x !== $y The result is true when both variables are not equal and not have same data type.
Greater than > ($x > $y) The result is true when the left hand side variable is bigger than right hand side variable.
Less than < ($x < $y) The result is true when the left hand side variable is smaller than right hand side variable.
Greater than or equal to >= ($x >= $y) The result is true when the left hand side variable is bigger or equal to the right hand side variable.
Less than or equal to <= ($x <= $y) The result is true when the left hand side variable is smaller or equal to the right hand side variable.

Example 2 – PHP Comparison Operators Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = 15;
		$y = 15;
		echo "Both the variables are equal - ";
		var_dump($x == $y);
		echo "<br>";
		echo "Both the variables are equal and have same data type - ";
		var_dump($x === $y);
		echo "<br>";
		echo "Both the variables are not equal - ";
		var_dump($x != $y);
		echo "<br>";
		echo "Both the variables are not equal and do not have same data type - ";
		var_dump($x !== $y);
		echo "<br>";
		echo "The left side variable is greater than right side variable - ";
		var_dump($x > $y);  
		echo "<br>";
		echo "The left side variable is smaller than right side variable - ";
		var_dump($x < $y);  
		echo "<br>";
		echo "The left side variable is greater or equal to the right side variable - ";
		var_dump($x >= $y); 
		echo "<br>";
		echo "The left side variable is smaller or equal to the right side variable - ";
		var_dump($x <= $y);
	?>
  </body>
</html>

In the above examples, perform the comparison operators on the variables and shows the result of those operations on the web page.

Comparison Operator
Fig.2 – Comparison Operator.

PHP Assignment Operators

To set the right hand side value to the left hand side variable, PHP Assignment Operators are used.

The advance assignment operator you can perform calculation and assigment at the same.

Operators Symbol Syntax Description
Equal = $x = $y The value of the right hand side variable is set to the left hand side variable.
Addition += ($x += $y) The sum of both variables is assigned to the left side variable.
Subtraction -= or <> $x -= $y The difference between both variables is assigned to the left side variable.
Multiplication *= $x *= $y The product of the variables is assigned to the left side variable.
Division /= ($x /= $y) The division of variables is assigned to the left side variable.
Modulus %= ($x %= $y) The remainder of the division variable is assigned to the left side variable.

Example 3 – PHP Assignment Operators Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = 20;
		$y = 15;
		echo "The value of variable is  ";
		echo $x = $y;
		echo "<br>";
		echo "The value of variable after addition is  ";
		echo $x += $y;
		echo "<br>";
		echo "The value of variable after subtraction is  ";
		echo $x -= $y;  
		echo "<br>";
		echo "The value of variable after multiplication is  ";
		echo $x *= $y;  
		echo "<br>";
		echo "The value of variable after division is   ";
		echo $x /= $y;  
		echo "<br>";
		echo "The value of variable after modulus is   ";
		echo $x %= $y;
	?>
  </body>
</html>

In the above examples, we perform the assigment operators on the variables and shows the result of those operations on the web page.

Here the value of $x is 20 but after applying the equal operator the value of $x changed by the value of $y(20).

Assignment Operators
Fig.3 – Assignment Operators.

PHP Increment / Decrement Operators

To increase the value of a variable by one than Increment Operators are used. And to decrease the value of a variable by one than Decrement Operators are used. Syntax of this operator is very simple.

Operators Symbol Syntax Description
Pre-increment ++ ++$x The value of the variable is increased by one before the evaluation of an expression.
Post-increment ++ $x++ The value of the variable is increased by one after the evaluation of an expression.
Pre-decrement – – – -$x The value of the variable is decreased by one before the evaluation of an expression.
Post-decrement – – $x- – The value of the variable is decreased by one after the evaluation of an expression.

Example 4 – PHP Increment / Decrement Operators Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = 10;
		echo "The value of variable is  ";
		echo $x;
		echo "<br>";
		echo "The value of variable in case of pre-increament operator is  ";
		echo ++$x;
		echo "<br>";
		echo "The value of variable in case of post-increament is  ";
		echo $x++;
		echo "<br>";
		echo "The value of variable after the post-increament is  ";
		echo $x;
		echo "<br>";
		echo "The value of variable in case of pre-decreament is  ";
		echo --$x;  
		echo "<br>";
		echo "The value of variable in case of post-decreament is  ";
		echo $x--;  
		echo "<br>";
		echo "The value of variable after post-decreament is  ";
		echo $x;
		echo "<br>";
	?>
  </body>
</html>

In the above examples, we perform the increament/decreament operators on the variables and shows the result of those operations on the web page.

Here the value of $x is 10 but after applying the increament/decreament operator the value of $x changed by one(1).

Increment Decrement Operators
Fig.4 – Increment Decrement Operators

PHP Logical Operators

PHP Logical Operators are used in decision-making statement. Normally you can use one statement in the if logic. But in case if you have more complex test condition then you can combine multiple comparison statements with the help of logical operators.

Operators Symbol Syntax Description
And and, && $x and $y It will return true when both the condition is valid.
Or or || $x or $y It will return true when one of the condition is valid.
Xor xor $x xor $y It will return true when one of the condition is valid, but not the both condition.
Not ! !$x It will return true when the condition is not true.

PHP Logical Operators

PHP Logical Operators are used in decision-making statement. Normally you can use one statement in the if logic. But in case if you have more complex test condition then you can combine multiple comparison statements with the help of logical operators.

Example 5 – PHP Increment / Decrement Operators Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = 20;
		$y = 30;
		if ($x == 20 and $y == 30) 
		{
			echo "And Logic Is True";
		}
		echo "<br>";
		if ($x == 100 or $y == 30) 
		{
			echo "Or Logic Is True";
		}
		echo "<br>";
		if ($x == 20 xor $y == 70) 
		{
			echo "Xor Logic Is True";
		}
		echo "<br>";
		if ($x !== 40) 
		{
			echo "And Logic Is True";
		}
	?>
  </body>
</html>

In the above examples, we perform the Logical operators on the variables and shows the result of those operations on the web page.

Logical Operators
Fig.5 – Logical Operators.

PHP String Concatenation Operators

PHP String Concatenation Operator is used to combine the two strings together. Concatenation Operator is also known as dot(.) operator.

Example 6 – PHP String Concatenation Operators Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
    
  <body>
	<?php
		$x = "I am at home. ";
		$y = "I want some food to eat.";
		
		echo $x.$y;
		echo "<br>";
		
		$a = "Today is a hot day. ";
		$b = "So i have to drink more water.";
		
		echo $a.$b;
	?>
  </body>
</html>

In the above examples, we perform the String Concatenation operators on the variables and shows the result of those operations on the web page.

PHP Concatenation Operators
Fig.6 – PHP Concatenation Operators.
You can leave a response, or trackback from your own site.

One Response to “PHP Operator with Example Code for Arithmetic, Comparison, Assignment, Increment, Decrement And Logical l and Conditional”

  1. I dugg some of you post as I thought they were handy very useful

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape