PHP Operators

PHP Operators

PHP Operators


Operators are used to manipulating or performing 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
  • Comparison Operators
  • Logical operators

Arithmetic Operators - Php supports the following arithmetic operators :

OperatorExampleDescription
+6 + 3 will give 9Addition of the two operands
-6 - 3 will give 3Subtraction of the second operand from the first
*6 * 3 will give 18Multiplying of the two operands
/6 / 3 will give 3Separating of the second operand from the first
%6 % 3 will give 0The 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 preference 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.

OperatorNameDescription
$x ++Post-incrementReturns $x, then increments $x by one.
++$xPre-incrementIncrements $x by one, then returns $x.
--$xPre-decrementDecrements $x by one, then returns $x.
$x--Post-decrementReturns $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 do 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 =. The assignment statement means that the left operand gets 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"

OperatorExampleDescription
+=x += y is equivalent to x = x + yAdd the right operand to the left operand and assign the result to the left operand
-=x -= y is equivalent to x = x - ySubtracts the right operand from the left operand and assign the result to the left operand
*=x *= y is equivalent to x = x * yMultiply the right operand with the left operand and assign the result to the left operand
/=x /= y is equivalent to x = x / yDivide the left operand into the right operand and assign the result to the left operand
%=x %= y is equivalent to x = x % yTake modulus using two operands and assign the result to the left operand



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

Comparison operators are supported by PHP language.

OperatorExampleDescription
==x == yCheck if the value of two operands are equal or not, if yes then condition becomes true else false
!=x != yCheck if the value of two operands are equal or not, if not then condition becomes true else false
>x > yCheck if the value of the left operand is greater than the value of right operand, if yes then condition becomes true.
<x < yCheck if the value of the left operand is less than the value of right operand, if yes then condition becomes true.
>=x >= yCheck 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 < = yCheck 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.

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


Reactions

Post a Comment

0 Comments

close