PHP Pocket Reference, 2nd Edition (2009)

Control Structures

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.


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.


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 thecase expressions match the switch expression, the default case is executed.


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 breakstatement (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.


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 {


} 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 whilestatement.


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

for(start_expr; cond_expr; iter_expr) {



for(start_expr; cond_expr; iter_expr):



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.


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

foreach(array_expression as $value) {



foreach(array_expression as $value):



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) {



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