Summarize various PHP control statements. PHP control statement 1. IF statements are an important feature of most languages. they execute program segments based on conditions. PHP's IF statement is similar to C: if (expr) statement, as discussed in the expression
PHP control statement 1. IF statement
IF statements are an important feature of most languages. they execute program segments based on conditions. PHP's IF statement is similar to C:
- If (expr)
- Statement
As discussed in the expression, expr is calculated as its true value. If expr is TRUE, PHP executes the corresponding statement. if it is FALSE, ignore it.
If $ a is greater than $ B, the following example shows 'a is bigger than B ':
- If ($>$ B)
- Print "a is bigger than B ";
Generally, you want to execute more than one statement according to the condition. Of course, you do not need to add IF judgment to each statement. Instead, you can combine multiple statements into a statement group.
If statements can be nested in other IF statements, allowing you to flexibly execute various parts of the program with conditions.
PHP control statement 2. ELSE statement
Generally, you want to execute a statement when the specified conditions are met. if the conditions are not met, execute another statement. ELSE is used to do this. ELSE extends the IF statement and executes another statement when the IF statement expression is FALSE. For example, if $ a is greater than $ B, 'A is bigger than B 'is displayed. otherwise, 'A is
- NOT bigger than b’:
- if ($a>$b) {
- print "a is bigger than b";
- }
- else {
- print "a is NOT bigger than b";
- }
PHP control statement 3. ELSEIF statement
ELSEIF, as shown in the name, is a combination of IF and ELSE, similar to ELSE. it extends the IF statement to execute other statements when the IF expression is FALSE. But unlike ELSE, it only executes other statements when the ELSEIF expression is TRUE.
Multiple ELSEIF statements can be used in one IF statement. The first statement with the ELSEIF expression TRUE will be executed. In PHP 3, you can also write 'else if' (two words) and 'elseif' (one word. This is just a small difference in writing (if you are familiar with C, it is also), the results are exactly the same.
The ELSEIF statement is executed only when the IF expression and any earlier ELSEIF expression are both FALSE and the current ELSEIF expression is TRUE.
The following is an IF statement containing the nested formats of ELSEIF and ELSE:
- 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;
PHP control statement 4. WHILE statement
WHILE loop is a simple loop of PHP 3. As in C. The basic format of the WHILE statement is:
WHILE (expr) statement
The WHILE statement is very simple. It tells PHP to repeatedly execute nested statements as long as the WHILE expression is TRUE. Check the value of the WHILE expression at the beginning of each loop. Therefore, this execution will not terminate even if its value is changed in the nested statement, and until the loop ends (every time PHP runs a nested statement, it is called a loop ). Similar to the IF statement, you can enclose a group of statements in braces and execute multiple statements in the same WHILE loop:
WHILE (expr): statement... ENDWHILE;
In the following example, all numbers are 1 to 10:
/* Example 1 */
- $i=1;
- while ($i0);
The preceding loop is executed only once, because after the first loop, when the true value expression is checked, it is calculated as FALSE ($ I is not greater than 0) and the loop is terminated.
PHP control statement 5. FOR loop statement
A FOR loop is the most complex loop in PHP. As in C. The syntax of the FOR loop is:
FOR (expr1; expr2; expr3) statement
The first expression (expr1) is unconditionally calculated (executed) at the beginning of the loop ).
The expression expr2 is calculated for each loop. If the result is TRUE, the loop and nested statements continue to be executed. If the result is FALSE, the entire loop ends.
At the end of each loop, expr3 is calculated (executed). each expression can be empty. If expr2 is null, the number of cycles is not fixed (PHP defaults to TRUE, like C ). Do not end the loop unless you use a BREAK statement to replace the true value expression of.
Consider the following example. They all show numbers 1 to 10:
/* Example 1 */
- for ($i=1; $i10) {
- 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 find that in the FOR loop, you can use an empty expression in many cases.
In other languages, a foreach statement is used to traverse an array or hash table. PHP uses the while statement, list (), and each () functions to achieve this function.
PHP control statement 6. SWITCH selection statement
The SWITCH statement is like a series of IF statements for the same expression. In many cases, you want to compare the same variable (or expression) with many different values and execute different program segments based on different comparison results. This is the use of the SWITCH statement.
The following two examples use different methods to do the same thing. one uses a group of IF statements and the other uses a SWITCH statement:
/* Example 1 */
- if ($i == 0) {
- print "i equals 0";
- }
- if ($i == 1) {
- print "i equals 1";
- }
- if ($i == 2) {
- print "i equals 2";
- }
- /* example 2 */
- switch ($i) {
- case 0:
- print "i equals 0";
- break;
- case 1:
- print "i equals 1";
- break;
- case 2:
- print "i equals 2";
- break;
- }
The explain IF statement is an important feature of most languages. it executes program segments based on conditions. PHP's IF statement is similar to C: if (expr) statement as mentioned in the expression...