Payne's PHP Notes

Back to Payne's PHP Notebook.

2. Operators

Assignment, Arithmetic, Increment, and Decrement Operator

Operator Example Result x y
= $x = 7
$y = 2
7 2
+ $x + $y 9    
- $x - $y 5    
* $x * $y 14    
/ $x / $y 3.5    
% $x % $y 1    
- -$x -7    
+= $x += $y 9  
-= $x -= $y 7  
*= $x *= $y 14  
/= $x /= $y 7  
%= $x %= $y 1  
y++ $x = $y++   2 3
++y $x = ++$y 4 4
y-- $x = $y--   4 3
--y $x = --$y   2 2

Comparison Operators and Logical Operators

function trueOrFalse($input)
{
    if($input)
    {
        echo "True";
    }
    else
    {
        echo "False";
    }
}

// True and False are not case sensitive!
true!True!TRUE!tRue!

$x = 7;
Operator Example Result
  $x True
== $x == 7 True
  $x == 5 False
  $x == "7" True
=== $x === 7 True
$x === "7" False
!= $x != 7 False
$x != "7" False
!== $x !== 7 False
  $x !== "y" True
<> $x <> 7 False
  $x <> "7" False
> $x > 5 True
  $x > "5" True
< $x < 5 False
>= $x >= 5 True
<= $x <= 5 False
and
&&
True && False False
  True And True True
  True aNd True True
or
||
True || False True
xor True xor False True
! !True False
  !$x False