PHP Operators – Part2

Part 2: Operators in programming languages are symbols that help to perform different types of tasks like arithmetical, logical, relational etc

php operators

Comparison operators

Comparison operators compare two values (or expressions) and return either true or false.

Operators Name Example Meaning
== Equal $a == $b; Returns true if $a and $b are equal after type juggling, otherwise returns false
=== Identical $a === $b; Returns true if $a and $b are equal and they are of same type, otherwise returns false
!= Not equal $a – $b; Returns true if $a and $b are not equal after type juggling, otherwise returns false
<> Not equal $a * $b; Returns true if $a and $b are not equal after type juggling, otherwise returns false
!== Not identical $a / $b; Returns true if $a and $b are not equal, or they are not of same type; otherwise returns false
< Less than $a < $b; Returns true if $a is less than $b
> Greater than $a > $b; Returns true if $b is less than $a
<= Less than or equal to $a <= $b Returns true if $a is less than or equal to $b
>= Greater than or equal to $a >= $b Returns true if $a is greater than or equal to $b

Example

<?php
    $a = 4;
    $b = 5;
    $c = “4”;
    $d = 4;
    var_dump($a == $b);  // output: bool(false)
    var_dump($a === $d);  // output: bool(true)
    var_dump($a === $c);  // output: bool(false)
    var_dump($a != $b);  // output: bool(true)
    var_dump($a <> $b);  // output: bool(true)
    var_dump($a !== $d);  // output: bool(false)
    var_dump($a == $c);  // output: bool(true)
    var_dump($a > $b);  // output: bool(false)
    var_dump($a < $b);  // output: bool(true)
    var_dump($a >= $b);  // output: bool(false)
    var_dump($a <= $b);  // output: bool(true)
 ?>

 

Explanation

Left operand Comparison operator Right Operand Result Reason
$a=4; == $b=5; False $a is not equal to $b, so returns false
$a=4; == $c = “4”; True After type juggling, “4” becomes 4 in $c and $a is equal to $b, so returns true
$a=4; === $d = 4; True $a is equal to $d in value and in type
$a=4; === $c = “4”; False $a is equal to $c in value nut they are not same type, so returns false
$a=4; != $b=5; True $a is not equal to $b, so returns true
$a=4; <> $b=5; True $a is not equal to $b, so returns true
$a=4; !== $d = 4; False $a is not equal to $b, so returns false
$a=4; > $b=5; False $a is not greater than $b, so returns false
$a=4; < $b=5; True $a is less than $b, so returns false
$a=4; >= $b=5; False $a is less not greater than or equal to $b, so returns false
$a=4; <= $b=5; True $a is less than $b, so returns true

Error control operators

  • PHP supports a error control operator – at sign (@)
  • Placing before an expression, it suppresses displaying any error message if the expression might be generated any error.

Example

<?php
     echo $a;
     echo @$a;
 ?>

Explanation

In the above example, line no 1 are trying to print the variable $a even the variable has not been declared before. This line will show an error similar to: “undefined variable a in FILE_LOCATION on line 1”. If you add @ before that variable like line no 2, the error message will not display.

Execution operators

  • PHP supports one execution operator – backticks ().
  • The output will be returned and can be assigned to a variable.
  • PHP interpreter will execute the content inside the backticks as operating system command.
  • The content inside the backticks () are operating system dependent.

Example:

&lt;?php
     $output = <code>dir –a1</code>;
     echo $output;
 ?&gt;

Output:

If you run the above code in a windows system, it will show drive’s name, serial number where this script is located. It will also show the directory location of the file. If you’re using operating linux system, you can replace dir by 1s.

Increment and decrement operators

PHP has some increment and decrement operators. The following table lists those operators.

In the following table, $a=5.

Operators Name Meaning Example Result
++expression Pre-increment First increase the value of expression by 1, and then return it. ++$a 6
expression ++ Post-increment First return the value, and then increase the value of expression by 1. $a++ 5
– expression Pre-decrement First decrease the value of expression by 1, and then return it. –$a 4
expression – Post-decrement First return the value, and then decrease the value of expression by 1. $a– 5
  • Character (a-z and A-Z) variables can be incremented, but can’t be decremented.
    <?php
        $var = ‘a’;
        $var2 = ‘a’;
        echo ++$var;  // Output: b
        echo --$var2;  // Output: a
     ?>
  • When character increment occurs, z becomes aa, Z to AA, aa to ab etc
    <?php
        $var = ‘z’;
        $var2 = ‘Z’;
        $var3 = ‘aa’;
        echo ++$var;  // Output: aa
        echo ++$var2;  // Output: ZZ
        echo ++$var3;  // Output: ab
     ?>
  • Incrementing or decrementing other character variables bring no change.
  • Increment or decrement operators don’t work on boolean value.
    <?php
         $a = TRUE;
         echo var_dump($a++);  // output: boolean(true)
         $b = FALSE;
         echo var_dump($b++);  // output: output: boolean(false)
     ?>
  • Incrementing NULL will return NULL, decreasing NULL will return 1.
    <?php
         $a=NULL;
         echo var_dump($a++);  // output: NULL
         echo var_dump($a--);  // output: int(1)<b>
     </b>?>

Logical operators

  • Logical operators help to make decision whether it is true or false based on the value(s) or expression(s) it takes.
  • Logical operators are frequently used with control structures (ex. If statements).
Operators Name Example Meaning
and And $a and $b; Returns true if $a and $b are true, otherwise returns false.
&& And $a && $b; Returns true if $a and $b are true, otherwise returns false.
or Or $a or $b; Returns true if either $a or $b is true, otherwise returns false.
|| Or $a || $b; Returns true if either $a or $b is true, otherwise returns false.
! Not !$a; Returns true if $a is not true, otherwise returns false
XOR Xor $a xor $b; Returns true if either $a or $b is true, but not both; otherwise returns false

[Note: Though the above two AND operators do the same thing, their precedence are different. Same is true for two OR operators. Operator precedence is discussed below later.]

Please note that expression that evaluates to 0, empty string(“”), NULL, or undefined are expressed to false.

Example of Logical “And” operators

<?php
    $a = (true and true);  // true and true return true
    $b = (false and true);  // false and true return false
    $c = (true && true);  // true && true return true
    $d = (false && true);  // false && true return false
    echo var_dump($a);  // bool(true)
    echo var_dump($b);  // bool(false)
    echo var_dump($c);  // bool(true)
    echo var_dump($d);  // bool(false)
    $e = (“Sunday” && “”);  // true and false return false
    $f = (1 && “Sunday”);  // true and true return true
    $f = (0 && “Sunday”);  // false and true return false
    echo var_dump($a);  // bool(true)
    echo var_dump($b);  // bool(false)
    echo var_dump($c);  // bool(true)
    echo var_dump($d);  // bool(false)
?>

Example of Logical “Or” operators

<?php
    $a = (true or true);  // true or true return true
    $b = (false or true);  // false or true return true
    $c = (true || true);  // true || true return true
    $d = (false || true);  // false || true return true
    echo var_dump($a);  // bool(true)
    echo var_dump($b);  // bool(true)
    echo var_dump($c);  // bool(true)
    echo var_dump($d);  // bool(true)
    $e = (“Sunday” || “”);  // true or false return true
    $f = (1 || “Sunday”);  // true or true return true
    $g = (0 || “Sunday”);  // false or true return true
    echo var_dump($e);  // bool(true)
    echo var_dump($f);  // bool(true)
    echo var_dump($g);  // bool(true)
?>

 

Example of Logical “Not” operators

<?php
$a = true;
$b = false;
echo var_dump(!$a);  // bool(false)
echo var_dump(!$b);  // bool(true)

$e = “Sunday”;  // String expresses true
$f = 1;  // non zero number expresses true
$g = 0;   // Zero expresses false
echo var_dump(!$e);  // bool(false)
echo var_dump(!$f);  // bool(false)
echo var_dump(!$g);  // bool(true)
?>

 

Example of Logical “Xor” operators

<?php
$a = (true xor true);  // true xor true return false
$b = (false xor true);  // false xor true return true
echo var_dump($a);  // bool(true)
echo var_dump($b);  // bool(true)
$e = (“Sunday” xor “”);  // true xor false return true
$f = (1 xor “Sunday”);  // true xor true return false
$g = (0 xor “Sunday”);  // false xor true return true
echo var_dump($e);  // bool(true)
echo var_dump($f);  // bool(false)
echo var_dump($g);  // bool(true)
?>

 

String operators

There are two string operators-

Operators Name Example Equivalent to Meaning
. Concatenation operator $a . $b; Concatenation of $a and $b
.= Concatenating assignment operator $a .= $b; $a = $a . $b; It appends the arguments on the right side to its left side.
<?php
$a= “PHP ”;
$b = “MySQL”;
$c = $a . $b;  // Output: PHP MySQL
$a .=$b;  // Output: PHP MySQL
?>

 

Array operators

Operators Name Example Meaning
+ Union $a + $b Join array $a and array $b
== Equality $a == $b Returns true if array $a and $b has the same key/value pairs regardless of their order and data type
=== Identity $a === $b Returns true if array $a and $b has the same key/value pairs and their order and data type are identical also.
!= Inequality $a != $b Returns true if array $a and $b are not same.
<> Inequality $a <> $b Returns true if array $a and $b are not same.
!== Non-identity $a !== $b Returns true if array $a is not identical to array $b.

Example of Array union( + ) operator:

Output:
array(3) {  [0]=>
string(6) “Canada”
[1]=>
string(3) “USA”
[2]=>
string(6) “France”
}

Explanation:

The keys/index of array $a are 0 and 1. And, the keys of array $b are 0,1,2. As both have 2 common keys, so the array union operator takes the elements of left hand array for those keys and create a new array in line no 5.

If keys/indexes are different in two arrays, the union operator joins all the elements.

Example:

<pre>
<?php
    $a = array("first"=>"Canada",
               "second"=> "USA"
               );
    $b = array("third"=>"United Kingdom",
               "fourth"=> "German",
               "fifth"=> "France"
               );
    $x = $a + $b;
    var_dump($x);
?>
</pre>

Output:

array(5) {
["first"]=>
string(6) “Canada”
["second"]=>
string(3) “USA”
["third"]=>
string(14) “United Kingdom”
["fourth"]=>
string(6) “German”
["fifth"]=>
string(6) “France”
}

Example of equality( == ) operator:

<pre>
 <?php
$a = array("Canada", "USA");
$b = array(1=>"USA", “0”=>"Canada");
$x = $a == $b;
var_dump($x);  // true
?>
 </pre>

 

Output:

bool(true)
bool(false)

Explanation:

Both Canada and USA in arrays $a and $b have same keys, so, the equality operator returns true in line no 5. Please note that the data type of key 0 in array $a is integer and in array $b is string. Equality operator doesn’t care this.

Example of identity( === ) operator:

<pre>
 <?php
$a = array("Canada", "USA");
$b = array(1=>"USA", “0”=>"Canada");
$x = $a === $b;
var_dump($x);  // false
?>
</pre>

 

Output:

bool(false)

Explanation:

Both $a and $b arrays have same key/value pairs, but, the data type of key 0 in array $a is integer and in array $b is string. So, identity operator returns false at line no 5.

Example of inequality( != ) operator:

<pre>
 <?php
$a = array("Canada", "USA");
$b = array(1=>"USA", “0”=>"Canada");
$c = array(1=>"United Kingdom", “0”=>"German");
$x = $a != $b;
$y = $a != $c;
var_dump($x);  // false
var_dump($y);  // true
?>
 </pre>

 

 

Output:

bool(false)
bool(true)

Explanation:

Both arrays $a and $b have same key/value pairs , so, the inequality operator returns false in line no 6. Similarly, both arrays $a and $c have different key/value pairs, so, the inequality operator returns true in line no 7.

Example of inequality( <> ) operator:

<pre>
 <?php
$a = array("Canada", "USA");
$b = array(1=>"USA", “0”=>"Canada");
$c = array(1=>"United Kingdom", “0”=>"German");
$x = $a <> $b;
$y = $a <> $c;
var_dump($x);  // false
var_dump($y);  // true
?>
</pre>

 

Output:

bool(false)
bool(true)

Explanation:

Both arrays $a and $b have same key/value pairs , so, the inequality operator returns false in line no 6. Similarly, both arrays $a and $c have different key/value pairs, so, the inequality operator returns true in line no 7.

Example of Non-identity ( !== ) operator:

<pre>
 <?php
    $a = array("Canada", "USA");
$b = array("0"=>"Canada", 1=>"USA");
$c = array(0=>"Canada", 1=>"USA");
$x = $a !== $b;
$y = $a !== $c;
var_dump($x);  // true
var_dump($y);  // true
?>
</pre>

Output:

bool(true)
bool(false)

Explanation:

Both $a and $b arrays have same key/value pairs, but, the data type of key 0 in array $a is integer and in array $b is string. So, non-identity operator returns true at line no 6.
Again, both $a and $c arrays have same key/value pairs, and, the data type of keys in both arrays are similar. So, non-identity operator returns false at line no 7.

Operator precedence

What is operator precedence?

When two operators share an expression, then one operator executes before the other when the program is executed. This is called operator precedence.  For example 2+3*5 produces 17. Here multiplication operator has higher precedence then addition operator.

What is associativity?

When two operators have same precedence, then, for some operators the program is executed from left to right, and for some other operators the program is executed from right to left. This is called associativity of the operator. For example, 2*10/5 produces 4. Here, multiplication and division operators have same precedence and their assiciativity is left t

The following tables shows the operator precedence from highest to lowest[top to bottom].

Associativity Operators Additional Information
non-associative clone new clone and new
left [ array()
non-associative ++ – increment/decrement
right ~ – (int) (float) (string) (array) (object) (bool) @ types
non-associative instanceof types
right ! logical
left * / % arithmetic
left + – . arithmetic and string
left << >> bitwise
non-associative < <= > >= <> comparison
non-associative == != === !== comparison
left & bitwise and references
left ^ bitwise
left | bitwise
left && logical
left || logical
left ? : ternary
right = += -= *= /= .= %= &= |= ^= <<= >>= => assignment
left and logical
left xor logical
left or logical
left , many uses
‹‹ PHP Operators – Part1 : Previous Lesson Next Lesson: PHP Conditional Statements ››


Pin It

↑ Back to Top