PHP Notes - Flow of Control

There are a number of operations that help the flow of control in a PHP program. The most common are the if statement and the switch. There are also a number of looping structures, but preference is given in some texts to just the while statement since other forms such as the for can be emulated by the while.

The "if/else" Group

Standard if and else combinations exist in PHP. Look at the examples below for syntax.

The switch statement

The switch statement is quite powerful in PHP because the variable does not have to be an integer, but may be any string value. If the string in the $variable matches "stringvalue1" in the example below, the first group of PHP statements will be executed up to the break command. If there is no match with "stringvalue1" or "stringvalue2", the default statements are executed.

Looping Structures

In PHP, some standard looping structures are available although there is no "repeat/until" structure. One book even discourages the use of the for statement since the action can be accomplished with a while statement where the condition tests some counter within the loop.