Operators are used in PHP for many things. Often to control a loop or if statement, they can be useful when counting, mathematical reasons and more. Below are the basic operators used and also a shorter form of them so that you have them as reference for when I hit control structures (very soon). I won’t bother with examples as I think the simple ones are very straight forward and the others require examples using code I’ve not touched on yet. Below is more for a reference for now.

// plus
$a += $b same as $a = $a + $b
// minus
$a -= $b same as $a = $a - $b
// multiply
$a *= $b same as $a = $a * $b
$a /= $b same as $a = $a / $b
// concatenate (for use in strings to join two strings together)
$a .= $b same as $a = $a . $b
// increment
$a++ same as $a = $a + 1
// decrement (is that a word?!!)
$a-- same as $a = $a - 1
// the comparison operator (different to one equals sign, below means "is equal to")
$a == $b
// assignment operator, ie. $a becomes the value of $b - don't get this and the comparison operator mixed up!
$a = $b
// not equal to
$a != $b
// $a is less than $b
$a < $b // $a is less than or equal to $b $a <= $b // $a is greater than $b $a > $b
// $a is great than or equal to $b (note the equals sign is always to the right of the operation)
$a >= $b
// NOT operator, returns true if $a is false
// AND operator, returns true if both are true
$a && $b
// OR operator, returns true if either are true
$a || $b
// same as AND operator
$a AND $b
// same as OR operator
$a OR $b

You may also like...