Conditional Statements

In a conditional statement you instruct PHP to take different actions depending on the outcome of a test. For example, you might want PHP to check whether a variable is greater than 10 and, if so, print a message. This is all done with the if statement, which looks like this:

if (your condition) {

// action to take if condition is true } else {

// optional action to take otherwise

The your condition part can be filled with any number of conditions you want PHP to evaluate, and this is where the comparison operators come into their own. For example:

echo "10 or lower";

PHP looks at the condition and compares $i to 10. If it is greater than 10, it replaces the whole operation with 1; otherwise, it replaces it with 0. So, if $i is 20, the result looks like this:

echo "10 or lower";

In conditional statements, any number other than 0 is considered to be equivalent to the Boolean value true, so if (1) always evaluates to true. There is a similar case for stringsif your string has any characters in, it evaluates to true, with empty strings evaluating to false. This is important because you can then use that 1 in another condition through && or || operators. For example, if you want to check whether $i is greater than 10 but less than 40, you could write this:

echo "10 or lower";

If we 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:

echo "The variable is equal to 10!"; } else {

echo "The variable is not equal to 10";

That 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 identicalmore 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 are strings, both are integers, and so on).


It is common practice to put function calls in conditional statements rather than direct comparisons. For example:

If the do_something() function returns true (or something equivalent to true, such as a nonzero number), the conditional statement evaluates to TRue.

Was this article helpful?

0 0

Post a comment