PHP Operators

Operators are used to manipulate or perform operations on variables and values.

Let's see expression 2 + 3 = 5 . Here 2 and 3 are called operands and + is called operator.

The PHP language supports the following types of operators.

  • Arithmetic Operators
  • Increment/decrement Operators
  • Assignment Operators
  • Comparision Operators
  • Logical operators

Arithmetic Operators - Php supports the following arithmetic operators :

Operator Example Description
+ 6 + 3 will give 9 Addition of the two operands
- 6 - 3 will give 3 Subtraction of the second operand from the first
* 6 * 3 will give 18 Multiplying of the two operands
/ 6 / 3 will give 3 Separating of the second operand from the first
% 6 % 3 will give 0 The integer remainder of (6 / 3)

 Note:

   The result of the operator % will have the same sign as the first operand, the result of $a% $b will have the same sign as the $a.

   The operator "%" works only with integers, operands are converted to integers (by removing the fractional part) prior to the operation.

   You can use brackets to set some priority over the other operators. Changing priority by using brackets corresponds to the usual mathematical rules.

<?php

   echo   (6%4) ;

   // output 2


   echo   (6 % -4) ;

   // output 2


   echo   ( -6 % 4 ) ;

   // output -2


   echo   (6,95 % 4,9) ;

   // output 2


   echo   2 * ( 6 - 4 ) ;

   // output 4 (The first working code in brackets)


?>

Increment/decrement Operators - PHP supports C-style pre and post increment and decrement operators.

Operator Name Description
$x ++ Post-increment Returns $x, then increments $x by one.
++$x Pre-increment Increments $x by one, then returns $x.
--$x Pre-decrement Decrements $x by one, then returns $x.
$x-- Post-decrement Returns $x, then decrements $x by one.

  Note:

    The Increment/decrement operators operate only on variables and not on any value.

    The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected.

    Decrementing NULL values has no effect, but incrementing them results in 1.

<?php

   $x = 5 ;

   echo   (5 ++ ) ;

   // wrong code (The Increment/decrement operators not  work on value)


   echo   (++ $x) ;

   // output 6


   $a = 5;

   echo   ( $a ++ ) ;

   // output 5


   echo ($a);

   // output 6 


   $y = 8 ;

   echo   (-- $y) ;

   // output 7


   $z = 8;

   echo   ( $z -- ) ;

   // output 8


   echo ($z);

   // output 7    


?>

Assignment Operators - Basic assignment operator is =. Assignment statement means that the left operand get a value of the right expression.

In the same line, we can assign value to multiple variables, because the result of the assignment operator is self assigned value .

For example:

<?php

   $x = $b = $c = 5;

   echo   ( $x,  $b, $c) ;

   // output 6 


   $x = ($y = 4 + 2 ) + 5;

   echo ($x) ;

   // output 11 


   echo ($y)

    // output 6

?>

In addition to the basic assignment operator, there are "Combined operators"

Operator Example Description
+= x += y is equivalent to x = x + y Add the right operand to the left operand and assign the result to the left operand
-= x -= y is equivalent to x = x - y Subtracts the right operand from the left operand and assign the result to the left operand
*= x *= y is equivalent to x = x * y Multiply the right operand with the left operand and assign the result to the left operand
/= x /= y is equivalent to x = x / y Divide the left operand into the right operand and assign the result to the left operand
%= x %= y is equivalent to x = x % y Take modulus using two operands and assign the result to the left operand


Comparision Operators - Comparison operators are used to compare two values.

Comparison operators are supported by PHP language .

Operator Example Description
== x == y Check if the value of two operands are equal or not, if yes then condition becomes true else false
!= x != y Check if the value of two operands are equal or not, if not then condition becomes true else false
> x > y Check if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
< x < y Check if the value of the left operand is less than the value of right operand, if yes then condition becomes true.
>= x >= y Check if the value of the left operand is greater than or equal to the value of the right operand, if yes then condition becomes true.
< = x < = y Check if the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true.


Logical operators - Logical operators use to compare two values.

Logical operators supported by PHP language .

Operator Example Description
&& x && y Logical AND operator. If both of the operands are true then condition becomes true.
|| x || y If any of the two operands are true then condition becomes true.
! x ! y Logical NOT Operator. Reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.