PHP Operators

PHP Operators are used to perform operations on operands(values).
Operators takes one or more operands or expressions to perform some operations and give another operands as output.

Operators are classified into 3 types according to the number of values they take.

  1. Unary Operator: Unary Operators take only one value.
  2. Binary Operator: Binary Operators take two values.
  3. Ternary Operator: Ternary Operator take three values.
    There is only one ternary operator is ? :. Ternary operator is also called as conditional operator.

PHP Operators can be categorized in following :

  • Arithmetic operators
  • Assignment operators
  • Bitwise operators
  • Comparison operators
  • Error Control operator
  • Execution operators
  • Increment/Decrement operator
  • Logical operators
  • String operators
  • Array operators
  • Type operators

Arithmetic PHP Operators

These operators are used to perform arithmetic/mathematical operations.
Below table describes all arithmetic operators.

Operator Name Description Example
       + Addition Adds two operands $a + $b
       – Subtraction Subtracts two operands $a – $b
       * Multiplication Multiplies two operands $a * $b
       / Division Divides first operand by second $a / $b
      % Modulus Returns the remainder of division $a % $b
      ** Exponentiation Result of raising $a to the $b’th power

 

Assignment PHP Operators

The basic assignment operator is “=”.
It is the value assigning operator. After evaluating the expression on the right-hand side, it assigns the value to the variable on the left-hand side.
Following is list of assignment operators.

Operator Name Example
       = Assignment operator $marks = 10
      += Increment, then assign $A += $B (i.e. $A = $A + $B)
      – = Decrement, then assign $A -= $B (i.e. $A = $A – $B)
      * = Multiply, then assign $A *= $B (i.e. $A = $A * $B)
      /= Divide, then assign $A /= $B (i.e. $A = $A / $B)
      %= Modulus, then assigns $A %= $B (i.e. $A = $A % $B)

 

Bitwise PHP Operators

Bitwise operators allow evaluation and manipulation of specific bits within an integer.
Following is the list of bitwise operators.
Assume $a = 4 and $b = 5;

Operator Name Example Output
       & AND $c = $a & $b (0100 & 0101) $c = 4
       | OR(inclusive or) $c = $a | $b $c = 5
       ^ XOR(exclusive or) $c = $a ^ $b 1
      >> Shift Right $c = $a >> $b $c = 0
      << Shift Left $c = $a << $b $c = 128
       ~ Not ~ a -5

 

Comparison PHP Operators

Comparison operators are used to compare two values.
The output of comparison operators is boolean values i.e. true or false.
Following table describes all comparison operators supported by PHP.
(assume a=5 and b=6)

Operator Name Description Example Output
       = = Is equal to Checks whether the two operands are equal or not. If so, it returns true. Otherwise, it returns false. $a = = $b false
      = = = Identical True if $a is equal to $b, and they are of the same type. $a = = = $b false
       != Is not equal to Checks whether the two operands are equal or not. If so, it returns true. Otherwise, it returns false. $a ! = $b true
       > Greater than Checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false $a > $b false
       < Less than Checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise, it returns false. $a < $b true
       >= Greater than or equal to Checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise, it returns false. 4a > = $b false
       < = Less than or equal to Checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise, it returns false. $a < = $b true
       <> = Not Equal Returns true if $a is not equal to $b $a <> = $b true
       !== Not identical True if $a is not equal to $b, or they are not of the same type. $a !== $b true
       < = > Spaceship Returns -1 when $a is less than $b, returns 0 when $a is equal to $b, returns 1 when $a is greater than $b. 1 < = > 1;
1 < = > 2;
2 < = > 1;
0 -1 1

 

Error Control PHP Operator

PHP supports only one error control operator: @(the at sign). @ operator works only on expressions.
When an @ operator is before any expression in PHP, then any error massage generated by that expression will be ignored.
You can prepend it to variables, constants etc. You can not prepend it to class definitions, conditional structures etc.
 

Execution Operator

PHP supports only one execution operator: (backticks).
The backtick operator is identical to shell_exec().
The backtick operator is not same as ‘ ‘(single quote).
PHP will attempt to execute contents of the backticks as a shell command.

If you will compiler above code then you will get massage like ” shell_exec() has been disabled for security reasons on line 2 “,
This is because the backtick operator is disabled when ‘safe mode’ is enabled.
 

Increment / Decrement PHP Operators

  • increment operator (++): This operator adds 1 to its operand.
    increment operator is divided into two types

    • post-increment operator –

      In this statement first the value of ‘$a’ will be printed on screen and then the value of ‘$a’ incremented by 1.
    • pre-increment operator –

      In this statement first the value of ‘$a’ will be incremented by 1 and then the incremented value will be printed on screen.
  •  

  • decrement operator (–): This operator subtracts 1 from its operand.

    decrement operator is divided into two types

    • post-decrement operator –

      In this statement first the value of ‘$a’ will be printed to screen and then the value of ‘$a’ decremented by 1.
    • pre-decrement operator –

      In this statement first the value of ‘$a’ will be decremented by 1 and then the decremented value will be printed on the screen.
  •  

    Logical PHP Operators

    This operator is used to test more than one condition.
    Following table describes all logical operators with example
    (assume 4x = 5 and $y = 10)

    Operator Name Description Example Output
           and logical And It returns true when both conditions are true ($x>5)and($y<5) false
            or logical OR It returns true when at-least one of the condition is true ($$x>=5) or (y>=10) true
             xor Xor True if either $x or $y is true, but not both. ($x>5)xor($y<5) false
           && And It returns true when both conditions are true ($x>5)and($y<5) false
            || Or It returns true when at-least one of the condition is true ($x>=5) or ($y>=10) true
             ! logical NOT It is used to reverse state of its operand. If a condition is true, then Logical NOT operator will make it false. !$a false

    There are two different variations depending on different precedences of “and” and “or” operators.
     

    String PHP Operators

    PHP supports following two string operators.

    1. Concatenation operator(.): It returns the concatenation of right and left arguments.
    2. Concatenating assignment operator(.=): It appends arguments on right side to the arguments on the left side.

    Output

     

    Array PHP Operators

    Operator Name Description Example
            + Union It returns union of two operand $a and $b. $a + $b
          == Equality True if $a and $b have the same key/value pairs. $a = = $b
           === Identity Returns true if $a and $b have the same key/value pairs in the same order and of the same types. $a === $b
           != Inequality It returns true if $a is not equal to $b. $a ! = $b
           <> Inequality It returns true if $a is not equal to $b. $a <> $b
           !== Non-identical True if $a is not equal to $b. $a !== $b

    Example of Union (+)

    Output

    Example of Equality(==) and Identity(===)
    Elements of array are equal for the comparison if they have the same key and value.

    Output

    Help others by sharing the content!

    Leave a Comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.