Книга: Fedora™ Unleashed, 2008 edition

Conditional Statements

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:

if ($i > 10) {
 echo "11 or higher";
} else {
 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:

if (1) {
 echo "11 or higher";
} else {
 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 strings: If your string has any characters in it, then 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:

if ($i > 10 && $i < 40) {
 echo "11 or higher";
} else {
 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. While 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:

if ($i = 10) {
 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 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 are strings, both are integers, and so on).

NOTE

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

if (do_something()) {

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

Оглавление книги


Генерация: 0.036. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз