# Control Structures

Programs are essentially a series of statements. Control structures, as their name implies, control how those statements are executed. Control structures are usually built around a series of conditions, such as "If the sky is blue, go outside and play." In this example, the condition is "If the sky is blue" and the statement is "go outside and play."

Control structures utilize curly braces ({ and }) to separate the groups of statements from the remainder of the program. Examples of common control structures follow; memorizing these will make your life much easier.

### if...elseif...else

The if...elseif...else construct executes a statement based on the value of the expression being tested. In the following sample if statement, the expression being tested is "\$a is equal to 10."

if (\$a == "10") {           // execute some code }

After \$a is evaluated, if it is found to have a value of 10 (that is, if the condition is true), the code inside the curly braces will execute. If \$a is found to be something other than 10 (if the condition is false), the code will be ignored and the program will continue.

To offer an alternative series of statements should \$a not have a value of 10, add an else statement to the structure, to execute a section of code when the condition is false:

if (\$a == "10") {           echo "a equals 10"; } else {           echo "a does not equal 10"; }

The elseif statement can be added to the structure to evaluate an alternative expression before heading to the final else statement. For example, the following structure first evaluates whether \$a is equal to 10. If that condition is false, the elseif statement is evaluated. If it is found to be true, the code within its curly braces executes. Otherwise, the program continues to the final else statement:

if (\$a == "10") {           echo "a equals 10";  } elseif (\$b == "8") {           echo "b equals 8"; } else {           echo "a does not equal 10 and b does not equal 8.";] }

You can use if statements alone or as part of an if...else or if...elseif...else statement. Whichever you choose, you will find this structure to be an invaluable element in your programs!

### while

Unlike the if...elseif...else structure, in which each expression is evaluated once and an action is performed based on its value of true or false, the while statement continues to loop until an expression is false. In other words, the while loop continues while the expression is true.

For example, in the following while loop, the value of \$a is printed on the screen and is incremented by 1 as long as the value of \$a is less than or equal to 5.

\$a = 0 // set a starting point while (\$a <= "5") {           echo "a equals \$a<br>";           \$a++; }

Here is the output of this loop: a equals 0

• a equals 1

• a equals 2

• a equals 3

• a equals 4

• a equals 5

### for

Like while loops, for loops evaluate the set of conditional expressions at the beginning of each loop. Here is the syntax of the for loop:

for (expr1; expr2; expr3) {           // code to execute }

At the beginning of each loop, the first expression is evaluated, followed by the second expression. If the second expression is true, the loop continues by executing the code and then evaluating the third expression. If the second expression is false, the loop does not continue, and the third expression is never evaluated.

Take the counting example used in the while loop, and rewrite it using a for loop:

for (\$a = 0; \$a <= "5"; \$a++) {           echo "a equals \$a<br>"; }

The output is the same as the while loop:

• a equals 0

• a equals 1

• a equals 2

• a equals 3

• a equals 4

• a equals 5

PHP Essentials, 2nd Edition
ISBN: 1931841349
EAN: 2147483647
Year: 2002
Pages: 74