PHP Operators

Operators in PHP

In general operators are of three types:

  • Unary operators
  • Binary Operators
  • Ternary operators

In every programming language, operator plays an important rules. They help to perform all mathematical and logical operations. Following are the set of operators supported in PHP language.

  • Arithmetic operators
  • Increment operators
  • Decrement operators
  • Assignment operator
  • Logical Operators
  • Bitwise Operators
  • Relational operators
  • Conditional or Ternary Operator.

 

Arithmetic Operators

Arithmetic operators includes operators like: addition, subtraction, multiplication, division etc. Following is the list of arithmetic operators.

Let $a=10 and $b=10 in the following cases.

Operators

Operator Type

Description

Examples

+

Binary

Adds two operands

$a+$b gives 20

-

Binary

Subtract 2nd operand from 1st

$a-$b gives 0

*

Binary

Multiply two operands

$a*$b gives 100

/

Binary

Divide 1st operands with 2nd

$a/$b gives 1

% (Modulas)

Binary

Finds the remainder

$a%$b gives 0

 

Increment Operator (++)

  • This operator increments the value of a variable by one.
  • It can be of two types
  • Pre-increment and Post-increment.
  • This is a unary operator, because it work with single operand.

Pre-increment: Syntax ++$val. In pre-increment the value of the variable first incremented by 1. Explained in the following example -

   <?php
      $val=10;
      echo ++$val; // Value of $val will first be incremented then will be displayed.
   ?>



Output: 11

Post-increment: Syntax $val++. In post-increment the value of the variable increases later. Follow the example below.

   <?php
      $val=10;
      echo $val++; // Value of $val will be displayed as 10 first then will increment.
      echo $val;
   ?>



Output: 10 11

 

Decrement Operator (--)

  • This operator decrements the value of a variable by one.
  • It can be of two types
  • Pre-decrement and Post-decrement.
  • This is a unary operator, because it work with single operand.

Pre-decrement: Syntax --$val. In pre-decrement the value of the variable first decremented by 1. Explained in the following example -

   <?php
      $val=10;
      echo --$val; // Value of $val will first be decremented then will be displayed.
   ?>



Output: 9

Post-decrement: Syntax $val--. In post-decrement the value of the variable decreases later. Follow the example below.

   <?php
      $val=10;
      echo $val--; // Value of $val will be displayed as 10 first then will decrement.
      echo $val;
   ?>



Output: 10 9

 

Assignment Operator (=)

  • Assigns the value from right side of the operator (=) to the left side variable.
  • A binary operator.
  • Works with two operands.

Example:

    <?php
       $val=10;// assigns or store 10 to the variable $val.
        echo $val;
    ?>

 

Output: 10

Variations of assignment operator: Following are the variations of assignment operator. For the following examples let $a=10 and $b=10 and $ret=0

Operator

Description

Example

=

Assigns value from right side to left side variables

$ret=$a+$b assigns value of $a+$b to $ret. Therefore $ret is now 20

+=

Assigns the sum of right and left sides to left side variable

$a+=$b equivalent to $a=$a+$b. Therefore $a is now 10

-=

Subtracts right side value from left side and assigns the result to the left side variable

$a-=$b equivalent to $a=$a-$b. Therefore $a is now 0

*=

Multiply both sides and assigns the value to the left side variable

$a*=$b equivalent to $a=$a*$b. Therefore $a is now 100

/=

Divides left side by right side and assigns value to the left side variable

$a/=$b equivalent to $a=$a/$b. Therefore $a is now 1

%=

Finds the modulus of left side by right side and assigns the result to left side variable

$a%=$b equivalent to $a=$a%$b. Therefore $a is now 0

 

Logical Operator

  • PHP has three logical operators logical and (&&), logical (||) and logical (!)
  • Expressions with logical operators always returns either TRUE(1) or FALSE(0)
  • Any non-zero value is treated as true.

Logical (&&) operator:

  • Returns true only if all the expressions are true.
  • Operator precedence left to right.
  • Once the compiler encounters a false expression it does not check for the rest in its right.
  • Used in situations where all conditions needed to be true.
  • Following is the truth table for && operator.

Truth table: 1 is treated as true and 0 is treated as false.

Exp1

Exp2

Exp1 && Exp2

Description

0

0

0

When compiler finds Exp1 as 0 it will not check Exp2

0

1

0

When compiler finds Exp1 as 0 it will not check Exp2

1

0

0

When compiler finds Exp1 as 1 it will check Exp2

1

1

1

When compiler finds Exp1 as 1 it will check Exp2

Example:

<?php
$var1=10;
$var2=20;
    if($var1 && $var2){
        echo $var1.’ ’.$var2;
    }else{
        echo ‘Condition is false’;
     }
?>

 

Output: 10 20.


Logical (||) operator:

  • Returns true, if at least one expression is true.
  • Operator precedence left to right.
  • Once the compiler encounters a true expression it does not check for the rest in its right.
  • Used in situations where at least one condition needed to be true.
  • Following is the truth table for || operator.

Truth table: 1 is treated as true and 0 is treated as false.

Exp1

Exp2

Exp1 && Exp2

Description

0

0

0

When compiler finds Exp1 as 0 it will check for Exp2

0

1

1

When compiler finds Exp1 as 0 it will check Exp2

1

0

1

When compiler finds Exp1 as 1 it will not check Exp2

1

1

1

When compiler finds Exp1 as 1 it will not check Exp2

 

Example:

<?php
$var1=10;
$var2=0;
    if($var1 || $var2){
        echo $var1.’ ’.$var2;
    }else{
        echo ‘Condition is false’;
     }
?>

 

Output: 10 0.


Logical (!) operator:

  • Returns true, if expression is false.
  • Returns false, if expression if true.
  • Operator precedence left to right.
  • Once the compiler encounters a true expression it does not check for the rest in its right.
  • Used in situations where the condition need to be checked negatively.
  • Following is the truth table for ! operator.

Truth table: 1 is treated as true and 0 is treated as false.

Exp1

!Exp1

Description

0

1

When compiler finds Exp1 as 0 it will return 1

1

0

When compiler finds Exp1 as 1 it will return 0

 

Example:

<?php
$var1=10;
    if(!$var1){
        echo $var1;
    }else{
        echo ‘Condition is false’;
     }
?>

 

Output: Condition is false.

 

Bitwise operators

  • Bitwise operators work with bits.
  • Binary numbers are native to computers.
  • We have binary logical operators and shift operators.
  • Bitwise operators are rarely used in high level languages like PHP

Operator

Meaning

|

Bitwise OR

&

Bitwise AND

~

Bitwise Negation or complement operator

^

Bitwise XOR

<<

Left shift operator

>>

Right shift operator

We are not going in to the depth of Bitwise operator, because they are rarely used in PHP.

 

Relational Operators:

  • Use to used to compare values.
  • Always returns boolean value either true of false.
  • Used in conditional situations.

Relational operator: For the following examples let $a=10 and $b=’10’

Operator

Description

Example

<

Check whether left side is less than right side

$a<$b, false

<=

Check whether left side is lest than or equal to right side

$a<=$b, true

>

Check whether left side is greater than right side

$a>$b, false

>=

Check whether left side is greater than or equal to right side

$a>=$b, true

==

Check whether both sides are equal of not

$a==$b, true

!= or <>

Returns true if both sides are not equal

$a!=$b, false

===

Check for equality(identical) with case sensitivity and data-type

$a===$b, false, because $a is numeric but $b is string

!==

Check for inequality(not identical) with case sensitivity and data-type

$a!==$b, true

 

Conditional Operator:

  • Works like an if else conditional statement.
  • known as ternary operator.
  • works with three expressions.


Syntax: Expression1 ? Expression2 : Expression3

Execution process: Expression1 is the conditional part. If Expression1 is true then Expression2 will execute. If Expression1 is false then Expression3 will execute.

Check Page Rank