1, if statement
An If statement is an important feature in most languages, and it executes the program segment according to the conditions. PHP's If statement is similar to C:
if (expr)
Statement
As discussed in an expression, expr is computed as its true value. If expr is true, PHP executes the corresponding statement and ignores it if it is false.
If $a is greater than $b, the following example displays ' A is bigger than B ':
if ($a > $b)
Print "A is bigger than B";
Typically, you want to execute more than one statement based on a condition. Of course, you don't need to add an IF to each statement. Instead, you can compose multiple statements into a single statement group.
If statements can be nested within other if statements, allowing you to flexibly conditionally execute parts of the program.
2. Else statement
Usually you want to execute a statement when a particular condition is met, and the condition is executed by another statement. else is used to do this. Else expands the IF statement to execute another statement when the IF statement expression is false. For example, the following program executes if $a is greater than $b displays ' A is bigger than B ', otherwise displays ' A is not bigger than B ':
The code is as follows |
Copy Code |
if ($a > $b) { Print "A is bigger than B"; } else { Print "A is not bigger than B"; }
|
3, ElseIf statement
ELSEIF, as the name shows, is a combination of if and else, similar to else, which extends the IF statement to execute other statements when the IF expression is false. Unlike else, however, it executes other statements only when the ElseIf expression is also true.
You can use more than one ElseIf statement in an if statement. A statement with the first ElseIf expression of true will be executed. In PHP 3, you can also write ' else if ' (written in two words) and ' ElseIf ' (write a word) effect. It's just a small difference in writing (if you're familiar with C, it's also) and the result is exactly the same.
The ElseIf statement executes only if the IF expression and any previous ElseIf expression are false, and the current ElseIf expression is true.
The following is an if statement with a nested format containing ElseIf and else:
The code is as follows |
Copy Code |
if ($a ==5): Print "a equals 5"; Print "..."; ElseIf ($a ==6): Print "a equals 6"; print "!!!"; Else Print "A is neither 5 nor 6"; endif
|
4, while statement
The while loop is a simple loop of PHP 3. The same as in C. The basic format of the while statement is:
while (expr) statement
The while statement has a very simple meaning. It tells PHP to repeatedly execute nested statements as long as the while expression is true. The value of the while expression is checked at the beginning of each loop, so even if its value is changed within the nested statement, this execution will not terminate until the loop ends (each time PHP runs a nested statement called a loop). Like an If statement, you can enclose a set of statements in curly braces and execute multiple statements in the same while loop:
while (expr): statement ... Endwhile;
The following examples are exactly the same, with numbers 1 to 10 being played:
The code is as follows |
Copy Code |
/* Example 1 * * $i = 1; while ($i <=10) { Print $i + +; /* The printed value would be $i before the increment (post- Increment) * * * } /* Example 2 * * $i = 1; while ($i <=10): Print $i; $i + +; Endwhile;
|
5, do ... While statement
Do.. While is very similar to a while loop, it only checks to see if the expression is true at the end of each loop, rather than at the beginning of the loop. The main difference between it and a strict while loop is do. The first loop of the while must be performed (the truth expression is checked only at the end of the loop) without having to perform a strict while loop (the truth expression is checked at the beginning of each loop, and if False at the beginning, the loop terminates execution immediately).
The code is as follows |
Copy Code |
Do.. While loops have only one form: $i = 0; do { Print $i; while ($i >0);
|
The above loop executes only once, because after the first loop, when the truth expression is checked, it evaluates to FALSE ($i not greater than 0) and the loop execution terminates.
6, for Loop statement
The For loop is the most complex loop in PHP. The same as in C. The syntax for the For loop is:
for (EXPR1; expr2; expr3) statement
The first expression (EXPR1) is evaluated unconditionally at the beginning of the loop (execution).
Each time the loop, the expression expr2 is computed. If the result is TRUE, the loops and nested statements continue to execute. If the result is FALSE, the entire loop ends.
At the end of each loop, the EXPR3 is computed (performed). Each expression can be empty. Expr2 is empty, the number of loops is indeterminate (PHP defaults to True, like C). Don't do this unless you want to end the loop with a conditional break statement instead of a for truth expression.
Consider the following example. They all show numbers 1 through 10:
The code is as follows |
Copy Code |
/* Example 1 * * For ($i =1 $i <=10; $i + +) { Print $i; } /* Example 2 * * for ($i = 1;;; $i + +) { if ($i > 10) { Break } Print $i; } /* Example 3 * * $i = 1; for (;;) { if ($i > 10) { Break } Print $i; $i + +; }
|
Of course, the first example is obviously the best, but you can see that you can use an empty expression for many occasions in the For loop.
Other languages have a foreach statement used to traverse an array or hash table. PHP uses the While statement and the list (), each () function to achieve this function