Click here to view code image
if ($i > 10 && $i < 40) {
echo "11 or higher";
} else {
echo "10 or lower";
}
If you presume that $i is set to 50 , the first condition ($i, 10) is replaced
with 1 , and the second condition ($i < 40) is replaced with 0 . Those two
numbers are then used by the && operator, which requires both the left and
right operands to be true. Whereas 1 is equivalent to true, 0 is not, so the
&& operand is replaced with 0 , and the condition fails.
=, ==, ===, and similar operators are easily confused and often the source of
programming errors. The first, a single equal sign, assigns the value of the
right operand to the left operand. However, all too often you see code like
this:
Click here to view code image
if ($i = 10) {
echo "The variable is equal to 10!";
} else {
echo "The variable is not equal to 10";
}
This code is incorrect. Rather than checking whether $i is equal to 10 , it
assigns 10 to $i and returns true. What is needed is ==, which compares
two values for equality. In PHP, this is extended so that there is also ===
(three equal signs), which checks whether two values are identical, more than
just equal.
The difference is slight but important: If you have a variable with the string
value “10” and compare it against the number value of 10 , they are equal.
Thus, PHP converts the type and checks the numbers. However, they are not
identical. To be considered identical, the two variables must be equal (that is,
have the same value) and be of the same data type (that is, both strings, both
integers, and so on).
NOTE
It is common practice to put function calls in conditional statements rather
than direct comparisons. Here is an example:
Click here to view code image
if (do_something()) {
If the do_something() function returns true (or something equivalent to