As defined in the expression chapter, expr is evaluated by Boolean. If the value of expr is TRUE, PHP executes statement. if the value is FALSE, statement is ignored. For more information about which values are considered FALSE, see convert to a Boolean value.
If
(PHP 4, PHP 5)
The if structure is one of the most important features of many languages, including PHP. it allows code snippets to be executed according to conditions. The if structure of PHP is similar to that of C:
As defined in the expression chapter, expr is evaluated by Boolean. If the value of expr is TRUE, PHP executes statement. if the value is FALSE, statement is ignored. For more information about which values are considered FALSE, see convert to a Boolean value.
If $ a is greater than $ B, the following example shows a is bigger than B:
$b) echo "a is bigger than b";?>
You often need to execute more than one statement according to the conditions. of course, you do not need to add an if clause to each statement. You can add these statements to the statement group. For example, if $ a is greater than $ B, the following code displays a is bigger than B and assigns the value of $ a to $ B:
$b) { echo "a is bigger than b"; $b = $a;}?>
If statements can be infinitely nested in other if statements, which provides sufficient flexibility for conditional execution of different parts of the program.
Else
(PHP 4, PHP 5)
It is often necessary to execute a statement when a condition is met, but other statements when the condition is not met. this is exactly the else function. Else extends the if statement and runs the statement when the expression value in the if statement is FALSE. For example, the following code displays a is bigger than B when $ a is greater than $ B. Otherwise, it displays a is NOT bigger than B:
$b) { echo "a is greater than b";} else { echo "a is NOT greater than b";}?>
The else statement is executed only when the value of the expression in the if and elseif statements is FALSE.
Elseif/else if
(PHP 4, PHP 5)
Elseif is a combination of if and else, which is similar to the alias. Like else, it extends the if statement and can execute different statements when the original if expression value is FALSE. But unlike else, it only executes the statement when the conditional expression value of elseif is TRUE. For example, the following code displays a is bigger than B, a equal to B, or a is smaller than B based on the conditions:
$b) { echo "a is bigger than b";} elseif ($a == $b) { echo "a is equal to b";} else { echo "a is smaller than b";}?>
The same if statement can contain multiple elseif parts. The first elseif part with the TRUE (if any) expression is executed. In PHP, you can also write "else if" (two words), which is identical to "elseif" (one word. The meaning of syntactic analysis is slightly different (if you are familiar with the C language, the behavior is the same), but the bottom line is that the two will produce the same behavior.
The elseif statement is executed only when the previous if and all previous elseif expressions are set to FALSE and the current elseif expression is set to TRUE.
Note: elseif and else if are considered to be identical only when curly brackets are used in similar cases. If you use a colon to define the if/elseif condition, you cannot use two words of else if. otherwise, PHP will produce a parsing error.