PHP

Operators in PHP

Operators are used to perform some operations on data operators are broadly classified into 2 categories

  1. Unary operators
    An operator  that requires only one operant in unary operator.
    Ex: $a++
    in the above statement a is operant and ++ is operator
  2. Binary Operator
    An operator that require two operant is called as binary operator.
    Ex: $a+$b

We have 8 types of operators in PHP those are

  1. Arithmetic operators
    1. +
    2. *
    3. /
    4. %
    <?php    
    echo(10/3)."<br>";
    
    echo(10%3)."<br>";
    echo(-10%3)."<br>";     
    echo(10%-3)."<br>";
    
    echo(-10%-3)."<br>";
    
    $a=5;
    $b="psd";    
    $c=$a+$b;      
    echo $c;      
    ?>
    the output is 
    
    3.3333333333333
    1
    -1
    1
    -1
    5
  2. Assignment operators
    1. =
    2. +=
    3. -=
    4. *=
    5. /=
    6. %=
    7. .=
    <?php
    $a=($b=4)+5;
    echo $a."
    <?php
    $a=($b=4)+5;
    echo$a;
    echo$b;
    ?>
    The output is

    9
    4

  3. increment and decrement Operators
    1. ++(increases value by one)
    2. –(Decrease value by one)
      <?php
            
      $x=10;      
      echo $x++."--".++$x;      
      ?>
      
            
      Output is      
      10--12
  4. Relation operators
    1. <
    2. >
    3. <=
    4. >=
    5. == (will check only for content is same or not)
    6. !=
    7. <>
    8. === (will check content and type of content also)
    9. !==
  5. Logical operators  (it was used to frame a compound condition)
    1. &&
    2. ||

      In case of logical operators all the expressions will not be evaluated every time . It is based on the result of first condition.(($a>$b)&&($a>$c))

       

  6. Negation operators

    it used to multiply a number with “-“ operator$a=10;

    $b=-$a;

  7. Error Control operators  (@)

    when prepended with an expression, any warning messages that is being generated will gets suppressed.

  8. Bitwise operators

    Bit wise operators are 3 they are

    1. AND –&
    2. OR    — |
    3. XOR  — ^

      In this case bit wise operators both operant will converted to binary form and corresponding bitwise operation will be performed.

  9. Shift Operators
    1. Left Shift operator (<<)
    2. Right shift operator (>>)

      In this case of shift operators the first operant will be converted to binary form and corresponding shifts will happen based on second number of operant.EX:–

      echo (100>>2);

      echo (100<<2);

      output will be

      25

      400

  10. String / Concadination operator
    1. .
    2. .=
  11. concadination / ternary operator
    1. ?:
  12. Type operator
    1. “insaneof”
  13. New Operator
    1. new
  14. Scope operator
    1. ::
  15. De-referencing operator
    1. ->

You Might Also Like