Any php Tutorial script consists of a series of statements. A statement can be a value assignment statement, a function call, a loop, or even a (null statement) condition statement that does nothing. The statement usually ends with a semicolon. In addition, you can use curly brackets to encapsulate a group of statements into a group of statements. A statement group can be considered as a row of statements. This chapter describes various statement types.
If
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:
If (expr)
Statement
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 Boolean.
If $ a is greater than $ B, the following example shows a is bigger than B:
<? Php
If ($ a> $ B)
Print "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:
<? Php
If ($ a> $ B ){
Print "a is bigger than B ";
$ B = $;
}
?>
If statements can be infinitely nested in other if statements, which provides sufficient flexibility for conditional execution of different parts of the program.
Else
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:
<? Php
If ($ a> $ B ){
Print "a is bigger than B ";
} Else {
Print "a is not bigger than B ";
}
?>
The else statement is executed only when the value of the expression in the if and elseif statements is false (see elseif ).
Elseif
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:
<? Php
If ($ a> $ B ){
Print "a is bigger than B ";
} Elseif ($ a ==$ B ){
Print "a is equal to B ";
} Else {
Print "a is smaller than B ";
}
?>
Multiple elseif statements can exist in the same if structure. The elseif statement with the first expression value true (if any) will be 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, this is the same behavior), but the bottom line is that the two will produce the same behavior.
The elseif statement is executed only when the previous if or elseif expression value is false and the current elseif expression value is true.
Else
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:
<? Php
If ($ a> $ B ){
Print "a is bigger than B ";
} Else {
Print "a is not bigger than B ";
}
?>
The else statement is executed only when the value of the expression in the if and elseif statements is false (see elseif ).