16.8 Control Structures

Team-Fly    

 
Webmaster in a Nutshell, 3rd Edition
By Robert Eckstein, Stephen Spainhour
Table of Contents
Chapter 16.  PHP


The control structures in PHP are very similar to those used by the C language. Control structures are used to control the logical flow through a PHP script. PHP's control structures have two syntaxes that can be used interchangeably. The first form uses C-style curly braces to enclose statement blocks, while the second style uses a more verbose syntax that includes explicit ending statements. The first style is preferable when the control structure is completely within a PHP code block. The second style is useful when the construct spans a large section of intermixed code and HTML. The two styles are completely interchangeable, however, so it is really a matter of personal preference which one you use.

16.8.1 if

The if statement is a standard conditional found in most languages. Here are the two syntaxes for the if statement:

 if(  expr  ) {              if(  expr  ):  statements   statements  } elseif(  expr  ) {      elseif(  expr  ):  statements   statements  } else {               else:  statements   statements  }                        endif; 

The if statement causes particular code to be executed if the expression it acts on is true. With the first form, you can omit the braces if you only need to execute a single statement.

16.8.2 switch

The switch statement can be used in place of a lengthy if statement. Here are the two syntaxes for switch:

 switch(  expr  ) {         switch(  expr  ):   case  expr  :               case  expr  :  statements   statements  break;                    break;   default:                  default:  statements   statements  break;                    break; }                            endswitch; 

The expression for each case statement is compared against the switch expression and, if they match, the code following that particular case is executed. The break keyword signals the end of a particular case; it may be omitted, which causes control to flow into the next case. If none of the case expressions match the switch expression, the default case is executed.

16.8.3 while

The while statement is a looping construct that repeatedly executes some code while a particular expression is true:

 while(  expr  ) {         while(  expr  ):  statements   statements  }                         endwhile; 

The while expression is checked before the start of each iteration. If the expression evaluates to true, the code within the loop is executed. If the expression evaluates to false, however, execution skips to the code immediately following the while loop. Note that you can omit the curly braces with the first form of the while statement if you only need to execute a single statement.

It is possible to break out of a running loop at any time using the break keyword. This stops the current loop and, if control is within a nested set of loops, the next outer loop continues. It is also possible to break out of many levels of nested loops by passing a numerical argument to the break statement (break n ) that specifies the number of nested loops it should break out of. You can skip the rest of a given loop and go onto the next iteration by using the continue keyword. With continue n , you can skip the current iterations of the n innermost loops.

16.8.4 do/while

The do/while statement is similar to the while statement, except that the conditional expression is checked at the end of each iteration instead of before:

 do {  statements  } while(  expr  ); 

Note that due to the order of the parts of this statement, there is only one valid syntax. If you only need to execute a single statement, you can omit the curly braces from the syntax. The break and continue statements work with this statement in the same way that they do with the while statement.

16.8.5 for

A for loop is a more complex looping construct than the simple while loop:

 for(  start_expr  ;  cond_expr  ;  iter_expr  ) {  statements  } for(  start_expr  ;  cond_expr  ;  iter_expr  ):  statements  endfor; 

A for loop takes three expressions. The first is the start expression; it is evaluated once when the loop begins. This is generally used for initializing a loop counter. The second expression is a conditional expression that controls the iteration of the loop. This expression is checked prior to each iteration. The third expression, the iterative expression, is evaluated at the end of each iteration and is typically used to increment the loop counter. With the first form of the for statement, you can omit the braces if you only need to execute a single statement.

The break and continue statements work with a for loop like they do with a while loop, except that continue causes the iterative expression to be evaluated before the loop conditional expression is checked.

16.8.6 foreach

A foreach loop is used to loop through an array. Here are both forms of the syntax:

 foreach(  array_expression  as $value) {  statements  } foreach(  array_expression  as $value):  statements  endforeach; 

This loops through the array_expression and assigns each value of the array to $value in turn . You can also get the key for each element with this syntax:

 foreach(  array_expression  as $key=>$value) {  statements  } 

The break and continue statements work with a foreach loop like they do with a for loop.


Team-Fly    
Top


Webmaster in a Nutshell
Webmaster in a Nutshell, Third Edition
ISBN: 0596003579
EAN: 2147483647
Year: 2002
Pages: 412

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net