only for RuBoard - do not distribute or recompile |
The order of execution is changed using program flow control statements. These statements are borrowed from everyday logical argumentative speech. I'm sure you have said something like "If I get this then I will do that, else I will have to wait." The PHP parser supports this form of logical decision, and executes code based on the result.
The main program flow statements are for, if, while, and switch. The for and while statements execute code for as long as a condition is true. The if statement executes certain code if a statement is true, or other code if the statement is false. The switch statement takes a look at an expression and executes the code in the group that matches the value of the expression.
When the condition is met, execute the code directly after the condition test. For example, if $a has a value of 3, assign a value of 5 to $b. If not, do nothing, leaving $b equal to 4:
$b = 4; if ($a ==3) $b = 5;
When the condition fails, execute the code directly after the else. In the following, if $a is not equal to 3, $b is set to a value of 6:
if ($a == 3) $b = 4; else $b = 6;
When the first condition fails, try the second condition and make a decision based on it. In the following, if $a is equal to 3, then $b is set to 4. Otherwise, if $c is equal to 7, the $b is set to 8; if c is not equal to 7, then $b is set to 9:
if ($a == 3) $b = 4; elseif ($c == 7) $b = 8; else $b = 9;
Execute the code between the brackets while the condition in the parentheses evaluates to true. If the condition in parentheses evaluates to false, the code in the loop is not executed. In the following example, the $c will keep incrementing until $b is equal to zero (ten times). The variable $b is decremented once through the loop:
$b = 10; while ($b > 0 ) { $c = $c +1; $b--; }
Execute the code in the loop, and then test the condition in parentheses. If the condition is true, the code is executed again. The code will always execute at least once:
$b = 10; do{ $c = $c +1; $b--; }while ($b > 0);
This is a three-part statement, with the parts separated by semicolons. The first part is an expression that is executed only once, and before the conditional test in the middle part is evaluated, the conditional test is made. If the test returns true, the code after the closing parentheses is executed. If the code after the closing parentheses is enclosed in brackets, all the code between the brackets is executed. If the test returns false, the bracketed code is not executed. At the end of the execution of the code between brackets, the expression in the third part is executed. At this point, the conditional test is made again, and the cycle repeats.
In the following example, $a is set to 0, the test is made, $b is incremented by 5, and then $a is incremented by one. When $a reaches the value of 6, program flow picks up with the statement after the for loop, causing $c to be set to 8:
for ($a = 0 ; $a < 6 ; $a++ ) { $b += 5; /* more statements can follow */ } $c = 8;
Each of the three parts can be empty. If the conditional test portion is empty, PHP always determines the result of the test is true, and will execute the code between the brackets until the loop is exited using a break or the script is ended with an exit:
for ( $a = 0 ; ; $a++) { $b++; if ($a > 10 ) break; if ($c == 15 ) exit; }
A switch evaluates the expression in parentheses and executes the code under the case that has the same value. If none of the cases match the evaluation, the default code is evaluated. If no evaluation matches, and the default code does not exist, no code is evaluated.
In the following code, if $a is equal to 1, "One" will be printed. If $a is equal to 12, "Twelve" will be printed. If $a is equal to 99, then "Nine" will be printed. If $a is equal to 9, then "Nine" will be printed. In all other cases, "who knows " will be printed:
switch($a) { case 1: echo "One"; break; case 12: echo "Twelve"; break; case 99: echo "Nine "; /* it falls through the next statement*/ case 9: echo "Nine"; break; default: echo "who knows"; break; }
This statement terminates execution of a loop or switch statement. Refer to the for and switch explanations for examples of its use.
This statement causes the loop to immediately jump to the conditional test. If the conditional test evaluates to true, the loop is executed again. In the following example, $c is not incremented until $a is greater than 9 . When $a is greater than 9, the $c is incremented, and the loop is exited:
$a = 0; while (1) { $a++; if($a < 10) continue; $c ++; break; }
The require statement is replaced with the specified file. This happens only once. If you want to replace the statement with different files when executed in a loop, you must use the include statement. In the following example, the contents of 'include.inc' are placed in the file, and the file 'loop1.inc' is include the first time through the loop, and 'loop2.inc' is included the second time through the loop:
<?php require 'include.inc'; $in = 'loop2.inc'; for ( $a = 0 ; $a < 2 ; $a ++) { if ($a < 1 ) include 'loop1.inc'; else include $in; }
include and require statements can appear anywhere in the code. You must be careful with opening and closing quotes, parentheses, semicolons, and brackets. It might not be obvious why a program fails when you include a file. Sometimes you have to merge the different files using a text editor to see the error.
only for RuBoard - do not distribute or recompile |