PHP Operators

PHP Operators

An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

Unary Operator – requires one operand

Binary Operator – requires two operands

Ternary Operator – requires three operands

Precedence – defines the order of operations, which operations occur before others

Associativity – when operator precedence is equal the operation is evaluated from left to right or right to left based on the associativity

Illegal – operators that are of equal precedence but non-associative cannot be used next to each other

Parentheses – can be used to explicitly define the order of operation. They’re acceptable to make the code more readable even if the order of operations would have implicitly occurred the same way.

Jump to PHP Operators:
Arithmetic Operators
Assignment Operators
Bitwise Operators
Comparison Operators
Error Control Operators
Execution Operators
Incrementing/Decrementing Operators
Logical Operators
String Operators
Array Operators
Type Operators

Arithmetic Operators

Precedence Explanation Associativity
** exponentiation right
* / % multiplication, division, modulus left
+ – addition, subtraction left
<?php
$a = 10 ** 3;  // prints 1000
$b = 10 * 3;   // prints 30
$c = 10 / 3;   // prints 3.3333333333333
$d = 10 % 3;   // prints 1
$e = 10 + 3;   // prints 13
$f = 10 - 3;   // prints 7
?>

 

Assignment Operators

Precedence Explanation Associativity
= += -= *= **= /= .= %= &= |= ^= <<= >>= ??= The = sign does not mean equal, but rather “gets set to”.  When preceded by an arithmetic operator, do the operation first and then assign the value. right
<?php
$i = 10;  //$i is set to 10
$i += 3;  //$i is now set to 13
$i -= 2;  //$i is now set to 11
$i *= 4;  //$i is now set to 44

$i = 10; //reset $i to 10
$i **= 2; //$i is now set to 100 
$i /= 25; //$i is now set to 4
$i .= " hours"; //$i is now set to 4 hours

$i = 10; //reset $i to 10
$i %= 3; //$i is now set to 1
$i &= ; //bitwise AND then sets string
$i |= ; //bitwise OR then sets string
$i ^= ; //bitwise XOR then sets string
$i <<= ; //bitwise shift left then sets string
$i >>= ; //bitwise shift right then sets string

$w = null;
$x = null;
$y = 10;
$z = 20;
echo $w ?? $x ?? $y ?? $z; //evaluates to expression 2 if expression 1 is NULL, else evaluates to expression 1. This will output 10. ?>

 

Bitwise Operators

Evaluation and manipulation of specific bits in an integer.

Precedence Explanation Associativity
<< >> Shift the bits of the integer steps to the left or right (each step means “multiply by two”) left
& (And) Bits that are set in both $a and $b are set left
| (Inclusive Or) Bits that are set in either $a or $b are set left
^ (Exclusive Or) Bits that are set in $a or $b but not both are set left
~ (Not) Bits that are set in $a are not set and vice versa left
<?php
$one = 1; // binary 0001
$two = 2; // binary 0010
$fifteen = 15; //binary 1111

//AND
$one & $two; // evaluates to 0000. prints 0. 
$one & $fifteen; // evaluates to 0001. prints 1.

//Inclusive OR
$one | $two; // evaluates to 0011. prints 3.
$one & $fifteen; // evaluates to 1111. prints 15.

//Exclusive OR
$one ^ $two; // evaluates to 0011. prints 3.
$one ^ $fifteen; // evaluates to 1110. prints 14.

//NOT
~$one; // evaluates to 1110. prints -2.
~$two; // evaluates to 1101. prints -9.
~$fifteen; // evaluates to 0000. prints -16.

//<< >> SHIFT
$to_shift = 1;
echo "<br>";
echo $one << $to_shift; //starts at 0001. changes to 0010. prints 2
echo "<br>";
echo $one >> $to_shift; //starts at 0001. changes to 0000. prints 0
echo "<br>";
echo $two << $to_shift; //starts at 0010. changes to 0100. prints 4
echo "<br>";
echo $two >> $to_shift; //starts at 0010. changes to 0001. prints 1
echo "<br>";
echo $fifteen << $to_shift; //starts at 1111. changes to 11110. prints 30
echo "<br>";
echo $fifteen >> $to_shift; //starts at 1111. changes to 0111. prints 7
?>

 

Comparison Operators

Precedence Explanation Associativity
< <= > >= less than, less than or equal to, greater than, greater than or equal to non-associative
== != === !== <> <=> equal, not equal, identity, non-identity, not equal, spaceship non-associative
<?php
$a = 10;
$b = 20;

$a < $b; //true
$a <= $b; //true
$a > $b; //false
$a >= $b; //false

$a == $b; //false
$a != $b; //true
$a === $b; //false
$a <> $b; //true
$a <=> $b; // -1
?>

 

Error Control Operators

Precedence Explanation Associativity
@ Suppresses error messages N/A
<?php
$the_input = @$myfunction(); //suppresses error if $myfunction doesn't exist
$this_input = @$myexpression[$key]; //suppresses error if $myexpression doesn't exist
?>

 

Execution Operators

Precedence Explanation Associativity
content inside backticks get executed as a shell command N/A
<?php
$show_file = `cat myfile.txt`;
echo "$show_file";
?>

 

Incrementing/Decrementing Operators

Precedence Explanation Associativity
++ — C-style pre- and post-increment and decrement N/A
<?php
//$i++
$i = 1;
while ($i < 5) {
    echo $i++;
    //prints 1 2 3 4
}

//++$i
$i = 1;
while ($i < 5) {
    echo ++$i;
    //prints 2 3 4 5
}

//$i--
$i = 5;
while ($i > 1) {
    echo $i--;
    //prints 5 4 3 2
}

//--$i
$i = 5;
while ($i > 1) {
    echo --$i;
    //prints 4 3 2 1
}

?>

 

Logical Operators

Precedence Explanation Associativity
! (Not) True if $a is not true. N/A
&& (And) True if both $a and $b are true. left
|| (Or) True if either $a or $b are true. left
and (And) True if both $a and $b are true. left
xor (Xor)  True if either $a or $b are true, but not both. left
or (Or) True if either $a or $b are true. left
<?php
$a = 1;
$b = 2;

if ($a != $b) { echo "a is not equal to b"; }

if ($a && $b == 1) { echo "a and b are both equal to 1"; }

if ($a || $b == 1) { echo "a or b are equal to 1"; }

if ($a and $b == 1) { echo "a and b are both equal to 1"; }

if ($a xor $b == 1) { echo "a or b are equal to 1, but not both"; }

if ($a or $b == 1) { echo "a or b are equal to 1"; }
?>

 

String Operators

Precedence Explanation Associativity
. Concatenation. A new string is created. left
.= Concatenation assignment. An existing string is updated. right
<?php

$a = "hello";
$b = "world";
$hw = "";

echo $a . $b; //prints hello world
$hw = $a; //prints hello
$hw .= $b; // prints hello world

?>

 

Array Operators

Precedence Explanation Associativity
+ == === != <> !== Union, Equality, Identity, Inequality, Inequality, Non-Identity N/A
<?php
$a = array("a" => "hat", "b" => "shirt");
$b = array("a" => "pants", "b" => "socks", "c" => "shoes");

$c = $a + $b; //Union of $a and $b
var_dump($c); //prints array(3) { ["a"]=> string(3) "hat" ["b"]=> string(5) "socks" ["c"]=> string(5) "shirt" }
$c = $b + $a; //Union of $b and $a var_dump($c); //prints array(3) { ["a"]=> string(3) "pants" ["b"]=> string(5) "socks" ["c"]=> string(5) "shoes" } 

$a = array("gloves", "belt");
$b = array(1=>"belt", "0" => "gloves");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false) due to the order in the arr
var_dump($a != $b); // bool(false)
var_dump($a <> $b); // bool(false)
var_dump($a !== $b); // bool(true)

?>

 

Type Operators

Precedence Explanation Associativity
instanceof() Checks if an object is of a certain class N/A
<?php
class Car{}
class Boat{}

$a = new Car;

var_dump($a instanceof Car);  //returns bool(true)
var_dump($a instanceof Boat); //returns bool(false)

?>

 

Leave a Reply