1. The compare operator ==
To check two values for equal you use the == operator. On both sides of the
operator are the values to check. These can be variables and a constant value.
PHP compares these values and returns a boolean value, so the result is either
false or true. This value can be used later like in
control structures.
<?php
"john" == "doe"; // results in bool(false), but the result isn't used in any way
$check = "max" == $var; // checks the content and saves true or false in $check
var_dump('foo' == 'bar'); // outputs bool(false) for debugging
?>
2. Other compare operators
Besides == there are other compare operators. For numbers there are
several operators like <, <=, >
and >= which checks the same as they are written.
<?php
$var = 5 < 7; // is true
$var = 10 <= 10; // is true
$var = 9 > 9; // is false
?>
And there is the unequal operator != to check if two values are
not the same.
<?php
$var = 10 != 10; // false
$var = 0 != 1; // true, as 0 is not 1
?>
As enhancement to the compare operators == and !=
there are two operators === and !==. These do not
just check the values but also the types of the values to check.
In this case the string "10" are not the same as
the integer value 10 as they were with ==.
<?php
$var = 5 == "5"; // is true
$var = 5 === "5"; // is false, because int != string
$var = 'Max' == "Max"; // is true, althought they use single quote strings and double quote strings
$var = 'Max' === "Max"; // is also true, both string have the same content and are strings
?>
You need this to check for return values of functions which can return the
boolean value false but can also return a value which
may be something like false but aren't a boolean value
(like the integer 0).
<?php
$var = false == 10; // false
$var = false == 0; // true, althought the first one is boolean and the second one is a number
$var = false === 10; // as above, false
$var = false === 0; // this time its false as int != boolean
?>
3. Assignment operator vs. compare operator
A common error is to use an assignment operator instead of an compare operator just
because you miss a equal character =. The php scripts are technically
valid but they do not do what they should do.
<?php
$check = $name == 'Max'; // Checks the value agains 'Max' and saves the (boolean) result in $check
$check = $name = 'Max'; // saves the value of 'Max' in $name and use this result (the assignment) as
// the new value for the variable $check (which will be also 'Max')
?>
That may be frustrated if you have this in an if statement. You want to check a value but actually overwrites the variable with a new value. To solve this common error (which happends always...) you should write the value to check agains always on the left of the compare operator.
<?php
"Max" == $name;
?>
With this code its impossible to miss a equal character =.
If you miss it anyway you get an parse error which can
be easily be fixed. But try to fix an error where you miss a
= somewhere in your code.
<?php
$check = "Max" == $name; // checking and save true/false in $check
$check = $name = "Max"; // maybe not that what you want and hard to find
$check = "Max" = $name; // not possible, results in an parse error (and then you know where and how to fix)
?>