PHP operators
You’ll find operators in most – if not all – programming languages.
They’re usually a symbol or two and do something with two or more variables and/or values.
So what does that look like?
<?php
$a = 10;
$b = 20;
// The asterisk is the operator (multiply)
$c = 10 * 20;
// C = 200
?>
The example above is a simple multiplication arithmetic operator.
But there are actually lots of different types of operator in PHP:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
Arithmetic operators
These ones all concern mathematical operations.
Addition (+)
Simply adds to variables together.
<?php
$x = 5;
$y = 10;
$z = $x + $y;
// Z = 15
?>
You don’t have to use variables, you can type numbers directly too.
Subtraction (-)
As above but subtracting instead.
<?php
$x = 10;
$y = 5;
$z = $x - $y;
// Z = 5
?>
Multiplication (*)
This time it’s multiplication.
<?php
$x = 5;
$y = 10;
$z = $x * $y;
// Z = 50
?>
Division (/)
We divide with a forward slash.
<?php
$x = 10;
$y = 5;
$z = $x / $y;
// Z = 2
?>
Modulus (%)
The modulus is the remainder of a division. If a number divides completely into the other it’s 0.
<?php
$x = 10;
$y = 3;
$z = $x % $y;
// Z = 1
?>
That’s because 3 times 3 equals 9 and 10 minus 9 equals 1.
Exponentiation (**)
Exponentiation just means to the power of. Here’s 10 to the power of 5.
<?php
$x = 10;
$y = 5;
$z = $x ** $y;
// Z = 100000
?>
Assignment operators
Assignment operators write a value to a variable – or they assign a value to a variable.
Standard
This the standard way you’ll set a variable in PHP and you probably already know how.
$x = $y
The value of $x is now the same as the value of $y.
Addition
You can write an addition formular in a shorter way, like this.
<?php
$x = 10;
$y = 5;
$x += $y;
// X = 15
// Y = 5
?>
The value on the left equals whatever it was before plus the value of the variable or number on the right.
Subtraction
As above but the operator changes to this.
$x -= $y
Multiplication
As addition above, but the operator changes like this.
$x *= $y
Division
It’s this for division.
$x /= $y
Modulus
And finally, modulus.
$x %= $y
Comparison operators
These are the ones you’ll most often use in if statements to compare strings and numbers.
Equal (==) and Identical (===)
Checking whether two variables/numbers are equal/identical.
<?php
1 == 1; // true
"1" == 1; // true
"1" === 1; // false
1 == 2. // false
1 === 2. // false
?>
For the ones that look the same, a "1" is a string and a 1 is a number, these are the same == but not identical ===.
Not equal (!= and <>) and not identical (!==)
Checking whether two variables/numbers are not equal/identical.
<?php
1 != 1; // false
1 != 2; // true
"1" !== 1; // true
"1" !== 2; // true
?>
Greater than (>) and less than (<)
It’s exactly what you think it is.
<?php
5 > 10; // false
10 > 5; // true
5 < 10; // true
10 < 5; // false
5 > 5; // false
5 < 5; // false
?>
Greater than or equal to (>=) and less than or equal to (<=)
As above but with the equals too – remember to but the equals on the correct side of the greater/less than sign.
<?php
5 >= 10; // false
10 >= 5; // true
5 <= 10; // true
10 <= 5; // false
5 >= 5; // true
5 <= 5; // true
?>
Spaceship (<=>)
This is a much more interesting one. It returns a number based on whether the first number is less than, equal to or greater than the number on the right.
<?php
echo (5 <=> 10); // -1 (5 less than 10)
echo (5 <=> 5); // 0 (5 equal to 5)
echo (15 <=> 10); // 1 (15 greater than 10)
?>
Increment/Decrement operators
Here’s all of them in one.
<?php
$num = 5;
// Pre-increment
echo ++$num; // 6 (adds 1 then returns)
// Post-increment
echo $num++; // 6 (returns and then adds 1)
// $num = 7 now
// Post-decrement
echo $num--; // 7 (returns 7 and then subtracts 1)
// $num = 6 now
// Pre-decrement
echo $--num; // 5 (substracts 1 and then returns)
?>
Logical operators
If you’ve got two variables and they could be either true or false, you can use these to find out whether both or either are true/false for example.
And (and, &&)
Here’s a boolean example.
<?php
$x = true;
$y = false;
echo $x and $y; // false
echo $x && $y; // false
?>
Or (or, ||)
It’s not just true/false values, but numeric and string values can be compared too.
<?php
$x = 10;
$y = 20;
echo $x == 10 or $y == 10; // true (because one is true)
echo $x == 10 || $y == 10; // true (because one is true)
echo $x == 10 && $y == 10; // false (for comparison)
?>
Xor (xor)
If one or other is true, but not both.
<?php
$x = 10;
$y = 20;
echo $x == 10 xor $y == 10; // true
echo $x == 10 xor $y == 20; // false (because both are true)
?>
Not (!)
Often used to make a variable the opposite boolean value.
<?php
$x = false;
echo !$x; // true, because $x is false (the opposite)
?>
String operators
Concatenation (.)
Make $z equal to $x, a space and $y.
$z = $x . " " . $y
Concatenation assignment (.=)
As above, but shorter. Makes $x the value of itself plus a space and the value of $y.
$x .= " " . $y
Array operators
These ones are for comparing and combining arrays.
Union (+)
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
print_r($x + $y);
// Array ( [a] => red [b] => green [c] => blue [d] => yellow )
?>
If any of the the keys match, the first key-value pair is kept and any othes with the same key are dropped.
Equality (==) and identity (===)
Checks whether arrays are the same or identical.
$array1 == $array2
Means each array contains the same key-value pairs.
$array1 === $array2
Means each array contains the same key-value pairs in the same order and each item is the same type.
Inequality (!= and <>)
Check arrays arent’t the same.
$array1 != $array2
$array1 <> $array2
Non-identity (!==)
Check arrays aren’t identical.
$array1 !== $array2
Conditional assignment operators
Ternary (?:)
The tenary operator is like a single-lined if statement.
$output = $x ? $y : $z
If $x is true set $output to $y and if false to $z.
Null coalescing (??)
$z = $x ?? $y
If $x exists and is not null, $z is set to $x, otherwise it’s $y.
