diff --git a/language/control-structures.xml b/language/control-structures.xml index 048d57608c..fbe1e7509b 100644 --- a/language/control-structures.xml +++ b/language/control-structures.xml @@ -1,418 +1,421 @@ - - Control Structures + + Control Structures + + Any PHP script is built out of a series of statements. A statement + can be an assignment, a function call, a loop, a conditional + statement of even a statement that does nothing (an empty + statement). Statements usually end with a semicolon. In addition, + statements can be grouped into a statement-group by encapsulating a + group of statements with curly braces. A statement-group is a + statement by itself as well. The various statement types are + described in this chapter. + + + + <literal>if</literal> + + The if construct is one of the most important + features of many languages, PHP included. It allows for + conditional execution of code fragments. PHP features an + if structure that is similar to that of C: + + +if (expr) + statement + + + - Any PHP script is built out of a series of statements. A statement can - be an assignment, a function call, a loop, a conditional statement of - even a statement that does nothing (an empty statement). Statements - usually end with a semicolon. In addition, statements can be grouped - into a statement-group by encapsulating a group of statements with - curly braces. A statement-group is a statement by itself as well. The - various statement types are described in this chapter. - - - <literal>if</literal> - - - The if construct is one of the most important - features of many languages, PHP included. It allows for conditional - execution of code fragments. PHP features an if - structure that is similar to that of C: - - - if (expr) - statement - - - - As described in the section about expressions, expr is evaluated - to its truth value. If expr evaluates - to TRUE, PHP will execute statement, and if it - evaluates to FALSE - it'll ignore it. - - - The following example would display a is bigger - than b if $a is bigger - than $b: - - - if ($a > $b) - print "a is bigger than b"; - - - - Often you'd want to have more than one statement to be executed - conditionally. Of course, there's no need to wrap each statement - with an if clause. Instead, you can group - several statements into a statement group. - For example, this code would display a is bigger than - b if $a is bigger than - $b, and would then assign the value of - $a into $b: - - - if ($a > $b) { - print "a is bigger than b"; - $b = $a; - } - - - - If statements can be nested indefinitely within other - if statements, which provides you with complete - flexibility for conditional execution of the various parts of - your program. - - - <literal>else</literal> - - - Often you'd want to execute a statement if a certain condition - is met, and a different statement if the condition is not met. - This is what else is for. else - extends an if statement to execute a statement - in case the expression in the if statement - evaluates to FALSE. For example, the following - code would display a is bigger than b - if $a is bigger than $b, - and a is NOT bigger than b otherwise: - - - - if ($a > $b) { - print "a is bigger than b"; - } else { - print "a is NOT bigger than b"; - } - - - - The else statement is only executed - if the if expression evaluated to - FALSE, and if there were any elseif - expressions - only if they evaluated to FALSE - as well (see below). - - - <literal>elseif</literal> - - - elseif, as its name suggests, is a combination - of if and else. Like - else, it extends an if - statement to execute a different statement in case the - original if expression evaluates to - FALSE. However, unlike else, - it will execute that alternative expression only if the - elseif conditional expression evaluates to - TRUE. For example, the following code would - display a is bigger than b, - a equal to b or a - is smaller than b: - - - - 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"; - } - - - - - There may be several elseifs within the same - if statement. The first elseif - expression (if any) that evaluates to true - would be executed. In PHP, you can also write 'else if' (in two - words) and the behavior would be identical to the one of 'elseif' - (in a single word). The syntactic meaning is slightly different - (if you're familiar with C, this is the same behavior) but the - bottom line is that both would result in exactly the same behavior. - - - The elseif statement is only executed if the preceding - if expression and any - preceding elseif expressions evaluated to FALSE, and the current - elseif expression evaluated to TRUE. - - - Alternative syntax for control structures - + As described in the section about expressions, expr is evaluated + to its truth value. If expr evaluates + to TRUE, PHP will execute statement, and if it + evaluates to FALSE - it'll ignore it. + - PHP offers an alternative syntax for some of its control - structures; namely, if, - while, for, and - switch. In each case, the basic form of the - alternate syntax is to change the opening brace to a colon (:) and - the closing brace to endif;, - endwhile;, endfor;, or - endswitch;, respectively. - - - - <?php if ($a==5): ?> - A is equal to 5 - <?php endif; ?> - - - - - - In the above example, the HTML block "A = 5" is nested within an - if statement written in the alternative syntax. - The HTML block would be displayed only if $a is equal to 5. - - - The alternative syntax applies to else - and elseif as well. The following is an - if structure with elseif - and else in the alternative format: - - - - 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; - - + The following example would display a is bigger + than b if $a is bigger + than $b: + + +if ($a > $b) + print "a is bigger than b"; + + - - See also while, - for, and if for further examples. + Often you'd want to have more than one statement to be executed + conditionally. Of course, there's no need to wrap each statement + with an if clause. Instead, you can group + several statements into a statement group. For example, this code + would display a is bigger than b + if $a is bigger than + $b, and would then assign the value of + $a into $b: + + +if ($a > $b) { + print "a is bigger than b"; + $b = $a; +} + + - + + If statements can be nested indefinitely within other + if statements, which provides you with complete + flexibility for conditional execution of the various parts of your + program. + - - <literal>while</literal> + + <literal>else</literal> + + Often you'd want to execute a statement if a certain condition is + met, and a different statement if the condition is not met. This + is what else is for. else + extends an if statement to execute a statement + in case the expression in the if statement + evaluates to FALSE. For example, the following + code would display a is bigger than + b if $a is bigger than + $b, and a is NOT bigger + than b otherwise: + + +if ($a > $b) { + print "a is bigger than b"; +} else { + print "a is NOT bigger than b"; +} + + + The else statement is only executed if the + if expression evaluated to + FALSE, and if there were any + elseif expressions - only if they evaluated to + FALSE as well (see below). + + - - while loops are the simplest type of loop in PHP. - They behave just like their C counterparts. The basic form of a - while statement is: + + <literal>elseif</literal> + + elseif, as its name suggests, is a combination + of if and else. Like + else, it extends an if + statement to execute a different statement in case the original + if expression evaluates to + FALSE. However, unlike + else, it will execute that alternative + expression only if the elseif conditional + expression evaluates to TRUE. For example, the + following code would display a is bigger than + b, a equal to b + or a is smaller than b: + + +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"; +} + + + + + There may be several elseifs within the same + if statement. The first + elseif expression (if any) that evaluates to + true would be executed. In PHP, you can also + write 'else if' (in two words) and the behavior would be identical + to the one of 'elseif' (in a single word). The syntactic meaning + is slightly different (if you're familiar with C, this is the same + behavior) but the bottom line is that both would result in exactly + the same behavior. + + + The elseif statement is only executed if the + preceding if expression and any preceding + elseif expressions evaluated to + FALSE, and the current + elseif expression evaluated to + TRUE. + + - - - while (expr) statement - - - - - The meaning of a while statement is simple. - It tells PHP to execute the nested statement(s) repeatedly, - as long as the while expression evaluates - to TRUE. The value of the expression is - checked each time at the beginning of the loop, so even if this - value changes during the execution of the nested statement(s), - execution will not stop until the end of the iteration (each - time PHP runs the statements in the loop is one iteration). - Sometimes, if the while expression evaluates - to FALSE from the very beginning, the nested - statement(s) won't even be run once. - - - Like with the if statement, you can group - multiple statements within the same while loop - by surrounding a group of statements with curly braces, or by using - the alternate syntax: - - - - while (expr): statement ... endwhile; - - - - The following examples are identical, and both print numbers from - 1 to 10: - - - - /* 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; - - - - - - <literal>do..while</literal> - - - do..while loops are very similar to - while loops, except the truth expression is - checked at the end of each iteration instead of in the beginning. - The main difference from regular while loops - is that the first iteration of a do..while - loop is guarenteed to run (the truth expression is only checked - at the end of the iteration), whereas it's may not necessarily run - with a regular while loop (the truth expression - is checked at the beginning of each iteration, if it evaluates - to FALSE right from the beginning, the loop - execution would end immediately). + + Alternative syntax for control structures + + PHP offers an alternative syntax for some of its control + structures; namely, if, + while, for, and + switch. In each case, the basic form of the + alternate syntax is to change the opening brace to a colon (:) and + the closing brace to endif;, + endwhile;, endfor;, or + endswitch;, respectively. + + + <?php if ($a == 5): ?> + A is equal to 5 + <?php endif; ?> + + + + + In the above example, the HTML block "A = 5" is nested within an + if statement written in the alternative syntax. + The HTML block would be displayed only if $a is equal to 5. + + + The alternative syntax applies to else and + elseif as well. The following is an + if structure with elseif and + else in the alternative format: + + +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; + + + + + See also while, + for, and if for further examples. + + - - There is just one syntax for do..while loops: + + <literal>while</literal> + + while loops are the simplest type of loop in + PHP. They behave just like their C counterparts. The basic form + of a while statement is: + + +while (expr) statement + + + + + The meaning of a while statement is simple. It + tells PHP to execute the nested statement(s) repeatedly, as long + as the while expression evaluates to + TRUE. The value of the expression is checked + each time at the beginning of the loop, so even if this value + changes during the execution of the nested statement(s), execution + will not stop until the end of the iteration (each time PHP runs + the statements in the loop is one iteration). Sometimes, if the + while expression evaluates to + FALSE from the very beginning, the nested + statement(s) won't even be run once. + + + Like with the if statement, you can group + multiple statements within the same while loop + by surrounding a group of statements with curly braces, or by + using the alternate syntax: + + +while (expr): statement ... endwhile; + + + + + The following examples are identical, and both print numbers from + 1 to 10: + + +/* example 1 */ + +$i = 1; +while ($i <= 10) { + print $i++; /* the printed value would be + $i before the increment + (post-increment) */ +} - - - $i = 0; - do { - print $i; - } while ($i>0); - - +/* example 2 */ - +$i = 1; +while ($i <= 10): + print $i; + $i++; +endwhile; + + + + + + + <literal>do..while</literal> + + do..while loops are very similar to + while loops, except the truth expression is + checked at the end of each iteration instead of in the beginning. + The main difference from regular while loops is + that the first iteration of a do..while loop is + guarenteed to run (the truth expression is only checked at the end + of the iteration), whereas it's may not necessarily run with a + regular while loop (the truth expression is + checked at the beginning of each iteration, if it evaluates to + FALSE right from the beginning, the loop + execution would end immediately). + + + There is just one syntax for do..while loops: + + + +$i = 0; +do { + print $i; +} while ($i>0); + + + + The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to FALSE ($i is not bigger than 0) and the loop - execution ends. - - - Advanced C users may be familiar with a different usage of - the do..while loop, to allow stopping - execution in the middle of code blocks, by encapsulating - them with do..while(0), and using the break statement. - The following code fragment demonstrates this: - - - - do { - if ($i < 5) { - print "i is not big enough"; - break; - } - $i *= $factor; - if ($i < $minimum_limit) { - break; - } - print "i is ok"; + execution ends. + + + Advanced C users may be familiar with a different usage of the + do..while loop, to allow stopping execution in + the middle of code blocks, by encapsulating them with + do..while(0), and using the break + statement. The following code fragment demonstrates this: + + +do { + if ($i < 5) { + print "i is not big enough"; + break; + } + $i *= $factor; + if ($i < $minimum_limit) { + break; + } + print "i is ok"; + ...process i... - } while(0); - - + +} while(0); + + + + + Don't worry if you don't understand this right away or at all. + You can code scripts and even powerful scripts without using this + `feature'. + + - - Don't worry if you don't understand this right away or at all. - You can code scripts and even powerful scripts without using this - `feature'. - - - <literal>for</literal> - - - for loops are the most complex loops in - PHP. They behave like their C counterparts. The syntax of a - for loop is: - - - + + <literal>for</literal> + + for loops are the most complex loops in PHP. + They behave like their C counterparts. The syntax of a + for loop is: + + for (expr1; expr2; expr3) statement - + + + + + The first expression (expr1) is + evaluated (executed) once unconditionally at the beginning of the + loop. + + + In the beginning of each iteration, + expr2 is evaluated. If it evaluates to + TRUE, the loop continues and the nested + statement(s) are executed. If it evaluates to + FALSE, the execution of the loop ends. + + + At the end of each iteration, expr3 is + evaluated (executed). + + + Each of the expressions can be empty. + expr2 being empty means the loop should + be run indefinitely (PHP implicitly considers it as + TRUE, like C). This may not be as useless as + you might think, since often you'd want to end the loop using a + conditional break + statement instead of using the for truth + expression. + + + Consider the following examples. All of them display numbers from + 1 to 10: + + +/* 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++; +} + +/* example 4 */ + +for ($i = 1; $i <= 10; print $i, $i++) ; + + + + + Of course, the first example appears to be the nicest one (or + perhaps the fourth), but you may find that being able to use empty + expressions in for loops comes in handy in many + occasions. + + + PHP also supports the alternate "colon syntax" for + for loops. + + +for (expr1; expr2; expr3): statement; ...; endfor; + - - - The first expression (expr1) is evaluated - (executed) once unconditionally at the beginning of the loop. - - - In the beginning of each iteration, expr2 - is evaluated. If it evaluates to TRUE, the - loop continues and the nested statement(s) are executed. If it - evaluates to FALSE, the execution of the loop ends. - - - At the end of each iteration, expr3 - is evaluated (executed). - - - Each of the expressions can be empty. - expr2 being empty means - the loop should be run indefinitely (PHP implicitly - considers it as TRUE, like C). - This may not be as useless as you might think, since - often you'd want to end the loop using a conditional break statement - instead of using the for truth expression. - - - Consider the following examples. All of them display numbers from - 1 to 10: - - - - /* 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++; - } - - /* example 4 */ - - for ($i = 1; $i <= 10; print $i, $i++) ; - - - - - Of course, the first example appears to be the nicest one (or - perhaps the fourth), but you may find that being able to use empty - expressions in for loops comes in handy in - many occasions. - - - PHP also supports the alternate "colon syntax" for - for loops. - - - - for (expr1; expr2; expr3): statement; ...; endfor; - - - Other languages have a foreach statement to traverse an array or hash. PHP3 has no such construct; PHP4 does @@ -425,10 +428,8 @@ for (expr1; expr2; expr3) statement - <literal>foreach</literal> - PHP4 (not PHP3) includes a foreach construct, much like perl and some other languages. This simply gives an easy @@ -441,7 +442,6 @@ foreach(array_expression as $key => $value) statement - The first form loops over the array given by array_expression. On each loop, the value of @@ -449,13 +449,11 @@ foreach(array_expression as $key => $value) statement the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). - The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop. - When foreach first starts executing, the internal array pointer is automatically reset to the first element @@ -463,142 +461,135 @@ foreach(array_expression as $key => $value) statement reset before a foreach loop. - You may have noticed that the following are functionally identical: - -reset( $arr ); -while( list( , $value ) = each( $arr ) ) { - echo "Value: $value<br>\n"; + +reset ($arr); +while (list(, $value) = each ($arr)) { + echo "Value: $value<br>\n"; } -foreach( $arr as $value ) { - echo "Value: $value<br>\n"; +foreach ($arr as $value) { + echo "Value: $value<br>\n"; } The following are also functionally identical: - -reset( $arr ); -while( list( $key, $value ) = each( $arr ) ) { - echo "Key: $key; Value: $value<br>\n"; + +reset ($arr); +while (list($key, $value) = each ($arr)) { + echo "Key: $key; Value: $value<br>\n"; } -foreach( $arr as $key => $value ) { - echo "Key: $key; Value: $value<br>\n"; +foreach ($arr as $key => $value) { + echo "Key: $key; Value: $value<br>\n"; } - Some more examples to demonstrate usages: - + /* foreach example 1: value only */ -$a = array(1, 2, 3, 17); -foreach($a as $v) { +$a = array (1, 2, 3, 17); + +foreach ($a as $v) { print "Current value of \$a: $v.\n"; } /* foreach example 2: value (with key printed for illustration) */ -$a = array(1, 2, 3, 17); + +$a = array (1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach($a as $v) { - print "\$a[$i] => $k.\n"; + print "\$a[$i] => $k.\n"; } /* foreach example 3: key and value */ -$a = array( - "one" => 1, - "two" => 2, - "three" => 3, - "seventeen" => 17 + +$a = array ( + "one" => 1, + "two" => 2, + "three" => 3, + "seventeen" => 17 ); foreach($a as $k => $v) { - print "\$a[$k] => $v.\n"; + print "\$a[$k] => $v.\n"; } - - - <literal>break</literal> - + break ends execution of the current if, for, while, or switch structure. - break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. - - + $i = 0; while ($i < 10) { - if ($arr[$i] == "stop") { - break; /* You could also write 'break 1;' here. */ - } - $i++; + if ($arr[$i] == "stop") { + break; /* You could also write 'break 1;' here. */ + } + $i++; } /* Using the optional argument. */ + $i = 0; while ( ++$i ) { - switch ( $i ) { - case 5: - echo "At 5<br>\n"; - break 1; /* Exit only the switch. */ - case 10: - echo "At 10; quitting<br>\n"; - break 2; /* Exit the switch and the while. */ - default: - break; - } + switch ( $i ) { + case 5: + echo "At 5<br>\n"; + break 1; /* Exit only the switch. */ + case 10: + echo "At 10; quitting<br>\n"; + break 2; /* Exit the switch and the while. */ + default: + break; + } } - <literal>continue</literal> - continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration. - continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. - - - -while (list($key,$value) = each($arr)) { + + +while (list ($key, $value) = each ($arr)) { if ($key % 2) { // skip even members continue; } @@ -606,488 +597,448 @@ while (list($key,$value) = each($arr)) { } $i = 0; -while ( $i++ < 5 ) { - echo "Outer<br>\n"; - while ( 1 ) { - echo "  Middle<br>\n"; - while ( 1 ) { - echo "  Inner<br>\n"; - continue 3; - } - echo "This never gets output.<br>\n"; - } - echo "Neither does this.<br>\n"; +while ($i++ < 5) { + echo "Outer<br>\n"; + while (1) { + echo "  Middle<br>\n"; + while (1) { + echo "  Inner<br>\n"; + continue 3; + } + echo "This never gets output.<br>\n"; + } + echo "Neither does this.<br>\n"; } - - - <literal>switch</literal> - - - The switch statement is similar to a series - of IF statements on the same expression. In many occasions, - you may want to compare the same variable (or expression) with - many different values, and execute a different piece of code - depending on which value it equals to. This is exactly what the - switch statement is for. - - - The following two examples are two different ways to write the - same thing, one using a series of if statements, - and the other using the switch statement: - - - - if ($i == 0) { - print "i equals 0"; - } - if ($i == 1) { - print "i equals 1"; - } - if ($i == 2) { - print "i equals 2"; - } - - switch ($i) { - case 0: - print "i equals 0"; - break; - case 1: - print "i equals 1"; - break; - case 2: - print "i equals 2"; - break; - } - - - - - It is important to understand how the switch - statement is executed in order to avoid mistakes. The - switch statement executes line by line (actually, - statement by statement). In the beginning, no code is executed. - Only when a case statement is found with a value - that matches the value of the switch expression - does PHP begin to execute the statements. PHP continues to execute the - statements until the end of the switch block, - or the first time it sees a break statement. - If you don't write a break statement at the end - of a case's statement list, PHP will go on executing the statements - of the following case. For example: - - - - switch ($i) { - case 0: - print "i equals 0"; - case 1: - print "i equals 1"; - case 2: - print "i equals 2"; - } - - - - - Here, if $i equals to 0, PHP would execute all of the print - statements! If $i equals to 1, PHP would execute the last - two print statements, and only if $i equals to 2, you'd get the - 'expected' behavior and only 'i equals 2' would be displayed. So, - it's important not to forget break statements - (even though you may want to avoid supplying them on purpose under - certain circumstances). - - + + <literal>switch</literal> - In a switch statement, the condition is - evaluated only once and the result is compared to each - case statement. In an elseif - statement, the condition is evaluated again. If your condition is - more complicated than a simple compare and/or is in a tight loop, - a switch may be faster. - - - - The statement list for a case can also be empty, which simply - passes control into the statement list for the next case. - - - switch ($i) { - case 0: - case 1: - case 2: - print "i is less than 3 but not negative"; - break; - case 3: - print "i is 3"; - } - - - - - A special case is the default case. This case matches anything - that wasn't matched by the other cases. For example: - - - - switch ($i) { - case 0: - print "i equals 0"; - break; - case 1: - print "i equals 1"; - break; - case 2: - print "i equals 2"; - break; - default: - print "i is not equal to 0, 1 or 2"; - } - - - - - - The case expression may be any expression that - evaluates to a simple type, that is, integer or floating-point - numbers and strings. Arrays or objects cannot be used here unless - they are dereferenced to a simple type. - - + The switch statement is similar to a series of + IF statements on the same expression. In many occasions, you may + want to compare the same variable (or expression) with many + different values, and execute a different piece of code depending + on which value it equals to. This is exactly what the + switch statement is for. + - The alternative syntax for control structures is supported with - switches. For more information, see Alternative - syntax for control structures . + The following two examples are two different ways to write the + same thing, one using a series of if + statements, and the other using the switch + statement: + + +if ($i == 0) { + print "i equals 0"; +} +if ($i == 1) { + print "i equals 1"; +} +if ($i == 2) { + print "i equals 2"; +} - - +switch ($i) { + case 0: + print "i equals 0"; + break; + case 1: + print "i equals 1"; + break; + case 2: + print "i equals 2"; + break; +} + + + + + It is important to understand how the switch + statement is executed in order to avoid mistakes. The + switch statement executes line by line + (actually, statement by statement). In the beginning, no code is + executed. Only when a case statement is found + with a value that matches the value of the + switch expression does PHP begin to execute the + statements. PHP continues to execute the statements until the end + of the switch block, or the first time it sees + a break statement. If you don't write a + break statement at the end of a case's + statement list, PHP will go on executing the statements of the + following case. For example: + + +switch ($i) { + case 0: + print "i equals 0"; + case 1: + print "i equals 1"; + case 2: + print "i equals 2"; +} + + + + + Here, if $i equals to 0, PHP would execute all of the print + statements! If $i equals to 1, PHP would execute the last two + print statements, and only if $i equals to 2, you'd get the + 'expected' behavior and only 'i equals 2' would be displayed. So, + it's important not to forget break statements + (even though you may want to avoid supplying them on purpose under + certain circumstances). + + + In a switch statement, the condition is + evaluated only once and the result is compared to each + case statement. In an elseif + statement, the condition is evaluated again. If your condition is + more complicated than a simple compare and/or is in a tight loop, + a switch may be faster. + + + The statement list for a case can also be empty, which simply + passes control into the statement list for the next case. + + +switch ($i) { + case 0: + case 1: + case 2: + print "i is less than 3 but not negative"; + break; + case 3: + print "i is 3"; +} + + + + + A special case is the default case. This case matches anything + that wasn't matched by the other cases. For example: + + +switch ($i) { + case 0: + print "i equals 0"; + break; + case 1: + print "i equals 1"; + break; + case 2: + print "i equals 2"; + break; + default: + print "i is not equal to 0, 1 or 2"; +} + + + + + The case expression may be any expression that + evaluates to a simple type, that is, integer or floating-point + numbers and strings. Arrays or objects cannot be used here unless + they are dereferenced to a simple type. + + + The alternative syntax for control structures is supported with + switches. For more information, see Alternative syntax + for control structures . + + switch ($i): - case 0: - print "i equals 0"; - break; - case 1: - print "i equals 1"; - break; - case 2: - print "i equals 2"; - break; - default: - print "i is not equal to 0, 1 or 2"; - endswitch; - - - + case 0: + print "i equals 0"; + break; + case 1: + print "i equals 1"; + break; + case 2: + print "i equals 2"; + break; + default: + print "i is not equal to 0, 1 or 2"; +endswitch; + + + - + <function>require</function> - - The require statement replaces itself with - the specified file, much like the C preprocessor's - #include works. + The require statement replaces itself with + the specified file, much like the C preprocessor's + #include works. - - An important note about how this works is that when a file is - includeed or requireed, - parsing drops out of PHP mode and into HTML mode at the beginning - of the target file, and resumes PHP mode again at the end. For - this reason, any code inside the target file which should be - executed as PHP code must be enclosed within valid PHP start and end - tags. + An important note about how this works is that when a file is + includeed or requireed, + parsing drops out of PHP mode and into HTML mode at the beginning + of the target file, and resumes PHP mode again at the end. For + this reason, any code inside the target file which should be + executed as PHP code must be enclosed within valid PHP start and end + tags. - - require is not actually a function in PHP; - rather, it is a language construct. It is subject to some - different rules than functions are. For instance, - require is not subject to any containing - control structures. For another, it does not return any value; - attempting to read a return value from a - require call results in a parse error. + require is not actually a function in PHP; + rather, it is a language construct. It is subject to some + different rules than functions are. For instance, + require is not subject to any containing + control structures. For another, it does not return any value; + attempting to read a return value from a + require call results in a parse error. - - Unlike include, require - will always read in the target file, - even if the line it's on never executes. If - you want to conditionally include a file, use - include. The conditional statement won't - affect the require. However, if the line on - which the require occurs is not executed, - neither will any of the code in the target file be executed. + Unlike include, require + will always read in the target file, + even if the line it's on never executes. If + you want to conditionally include a file, use + include. The conditional statement won't + affect the require. However, if the line on + which the require occurs is not executed, + neither will any of the code in the target file be executed. - - Similarly, looping structures do not affect the behaviour of - require. Although the code contained in the - target file is still subject to the loop, the - require itself happens only once. + Similarly, looping structures do not affect the behaviour of + require. Although the code contained in the + target file is still subject to the loop, the + require itself happens only once. - - This means that you can't put a require - statement inside of a loop structure and expect it to include the - contents of a different file on each iteration. To do that, use an - include statement. - - - -require( 'header.inc' ); - - + This means that you can't put a require + statement inside of a loop structure and expect it to include the + contents of a different file on each iteration. To do that, use an + include statement. + + +require ('header.inc'); + + - - - Please note that both include and - require actually pull the contents of the - target file into the calling script file itself; they do not call - the target via HTTP or anything like that. So any variable set in - the scope in which the inclusion happens will be available within - the included file automatically, since it has effectively become a - part of the calling file. - - -require( "file.inc?varone=1&vartwo=2" ); /* Won't work. */ + Please note that both include and + require actually pull the contents of the + target file into the calling script file itself; they do not call + the target via HTTP or anything like that. So any variable set in + the scope in which the inclusion happens will be available within + the included file automatically, since it has effectively become a + part of the calling file. + + +require ("file.inc?varone=1&vartwo=2"); /* Won't work. */ $varone = 1; $vartwo = 2; -require( "file.inc" ); /* $varone and $vartwo will be available in file.inc */ - - +require ("file.inc"); /* $varone and $vartwo will be available in file.inc */ + + - - Don't be misled by the fact that you can require or include files - via HTTP using the Remote - files feature; the above holds true regardless. + Don't be misled by the fact that you can require or include files + via HTTP using the Remote + files feature; the above holds true regardless. - - In PHP3, it is possible to execute a return - statement inside a requireed file, as long as - that statement occurs in the global scope of the - requireed file. It may not occur within any - block (meaning inside braces ({}). In PHP4, however, this ability - has been discontinued. If you need this functionality, see - include. + In PHP3, it is possible to execute a return + statement inside a requireed file, as long as + that statement occurs in the global scope of the + requireed file. It may not occur within any + block (meaning inside braces ({}). In PHP4, however, this ability + has been discontinued. If you need this functionality, see + include. - <function>include</function> - - The include statement includes and evaluates - the specified file. + The include statement includes and evaluates + the specified file. - - An important note about how this works is that when a file is - includeed or requireed, - parsing drops out of PHP mode and into HTML mode at the beginning - of the target file, and resumes again at the end. For this reason, - any code inside the target file which should be executed as PHP - code must be enclosed within valid PHP start and end - tags. + An important note about how this works is that when a file is + includeed or requireed, + parsing drops out of PHP mode and into HTML mode at the beginning + of the target file, and resumes again at the end. For this reason, + any code inside the target file which should be executed as PHP + code must be enclosed within valid PHP start and end + tags. - - - This happens each time the include statement is - encountered, so you can use an include statement - within a looping structure to include a number of different files. - - - + + This happens each time the include statement + is encountered, so you can use an include + statement within a looping structure to include a number of + different files. + + $files = array ('first.inc', 'second.inc', 'third.inc'); for ($i = 0; $i < count($files); $i++) { include $files[$i]; } - - + + - - include differs from - require in that the include statement is - re-evaluated each time it is encountered (and only when it is - being executed), whereas the require - statement is replaced by the required file when it is first - encountered, whether the contents of the file will be evaluated or - not (for example, if it is inside an if statement whose - condition evaluated to false). + include differs from + require in that the include statement is + re-evaluated each time it is encountered (and only when it is + being executed), whereas the require + statement is replaced by the required file when it is first + encountered, whether the contents of the file will be evaluated or + not (for example, if it is inside an if statement whose + condition evaluated to false). - - Because include is a special language - construct, you must enclose it within a statement block if it is - inside a conditional block. + Because include is a special language + construct, you must enclose it within a statement block if it is + inside a conditional block. + + +/* This is WRONG and will not work as desired. */ - - - /* This is WRONG and will not work as desired. */ +if ($condition) + include($file); +else + include($other); - if ($condition) - include($file); - else - include($other); +/* This is CORRECT. */ - /* This is CORRECT. */ - - if ($condition) { - include($file); - } else { - include($other); - } - - +if ($condition) { + include($file); +} else { + include($other); +} + + - - In both PHP3 and PHP4, it is possible to execute a - return statement inside an - includeed file, in order to terminate - processing in that file and return to the script which called - it. Some differences in the way this works exist, however. The - first is that in PHP3, the return may not - appear inside a block unless it's a function block, in which case - the return applies to that function and not the - whole file. In PHP4, however, this restriction does not - exist. Also, PHP4 allows you to return values from - includeed files. You can take the value of the - include call as you would a normal - function. This generates a parse error in PHP3. + In both PHP3 and PHP4, it is possible to execute a + return statement inside an + includeed file, in order to terminate + processing in that file and return to the script which called + it. Some differences in the way this works exist, however. The + first is that in PHP3, the return may not + appear inside a block unless it's a function block, in which case + the return applies to that function and not the + whole file. In PHP4, however, this restriction does not + exist. Also, PHP4 allows you to return values from + includeed files. You can take the value of + the include call as you would a normal + function. This generates a parse error in PHP3. - - <function>include</function> in PHP3 and PHP4 - - Assume the existence of the following file (named - test.inc) in the same directory as the main - file: - + <function>include</function> in PHP3 and PHP4 + + Assume the existence of the following file (named + test.inc) in the same directory as the main + file: + <?php echo "Before the return <br>\n"; -if ( 1 ) { - return 27; +if (1) { + return 27; } echo "After the return <br>\n"; ?> - - - - - Assume that the main file (main.html) - contains the following: - + + + + Assume that the main file (main.html) + contains the following: + <?php -$retval = include( 'test.inc' ); +$retval = include ('test.inc'); echo "File returned: '$retval'<br>\n"; ?> - - - - - When main.html is called in PHP3, it will - generate a parse error on line 2; you can't take the value of an - include in PHP3. In PHP4, however, the - result will be: - + + + + When main.html is called in PHP3, it will + generate a parse error on line 2; you can't take the value of an + include in PHP3. In PHP4, however, the + result will be: + Before the return File returned: '27' - - - - - Now, assume that main.html has been altered - to contain the following: - + + + + Now, assume that main.html has been altered + to contain the following: + <?php -include( 'test.inc' ); +include ('test.inc'); echo "Back in main.html<br>\n"; ?> - - - - - In PHP4, the output will be: - + + + + In PHP4, the output will be: + Before the return Back in main.html - - However, PHP3 will give the following output: - + + However, PHP3 will give the following output: + Before the return 27Back in main.html Parse error: parse error in /home/torben/public_html/phptest/main.html on line 5 - - - - - The above parse error is a result of the fact that the - return statement is enclosed in a non-function - block within test.inc. When the return is - moved outside of the block, the output is: - + + + + The above parse error is a result of the fact that the + return statement is enclosed in a non-function + block within test.inc. When the return is + moved outside of the block, the output is: + Before the return 27Back in main.html - - - - - The spurious '27' is due to the fact that PHP3 does not support - returning values from files like that. - - + + + + The spurious '27' is due to the fact that PHP3 does not support + returning values from files like that. + - - Please note that both include and - require actually pull the contents of the - target file into the calling script file itself; they do not call - the target via HTTP or anything like that. So any variable set in - the scope in which the inclusion happens will be available within - the included file automatically, since it has effectively become a - part of the calling file. - - -include( "file.inc?varone=1&vartwo=2" ); /* Won't work. */ + Please note that both include and + require actually pull the contents of the + target file into the calling script file itself; they do not call + the target via HTTP or anything like that. So any variable set in + the scope in which the inclusion happens will be available within + the included file automatically, since it has effectively become a + part of the calling file. + + +include ("file.inc?varone=1&vartwo=2"); /* Won't work. */ $varone = 1; $vartwo = 2; -include( "file.inc" ); /* $varone and $vartwo will be available in file.inc */ - - - - +include ("file.inc"); /* $varone and $vartwo will be available in file.inc */ + + - - Don't be misled by the fact that you can require or include files - via HTTP using the Remote - files feature; the above holds true regardless. + Don't be misled by the fact that you can require or include files + via HTTP using the Remote + files feature; the above holds true regardless. - - See also readfile, - require, and virtual. + See also readfile, + require, and virtual. - +