Title: Flow Control ($$$)
Course: PHP/MySQL Course

flow, the if statement and the switch statement

Read pages 111 to 117 about flow of control, the if statement, the switch statement, and the ? operator.

Here are examples of if statements:

if( $age > 65) {
echo "you can retire now";
}

if( $quantity < 10 ) {
$total = $price * $quantity;
} else {
# compute with a 5% discount
$total = $price * $quantity * .95;
}

note that these examples always use left and right braces { } to surround the block of statements to be executed in the if or else case. It is good practice to ALWAYS use these braces with all if or else parts.

You should always indent the code within the braces. This will make your programs easier and is an industry standard.

Note about the ? operator,

this is a way of making a choice within an expression, it behaves like a if statement, but returns a value based on the test performed to the context of where it is used.

for example if we want to give a 5% discount for $quanity greater than 10 when we compute the $total as in the code above, we can replace all the code above with:

$total = $price * $quantity * ($quantity < 10)? 1.0 , 0.95;

1.0 will be used for the multiply if the $quantity is < 10

Question:
What will the following code print out?

$x = 5;
$y = $x - 2;

switch($y) {
case 1:
echo "blue";
break;
case 2:
echo "yellow";
break;
case 3:
echo "green";
break;
case 4:
echo "white";
break;
default:
echo "black";
}

- green

while statement

Read about the while statement on page 117 to 118.

The code inclosed in the { } for the loop is called the body of the loop.

The body of the loop will be repeated as long as the test condition is true.

The general pattern of a while loop is:

while( true/false expression ) {
statements in body of loop
}

Note that the body of the loop is never executed if the true/false expression is initially false.

If the statements in the body never change the result of the true/false expression, then you would loop forever (called an infinite loop).

usually there is one variable that is used in the true/false condition that controls the loop, this variable is changed within the body of the loop. This variable is refereed to as the loop control variable.

You need to learn how to look at code for a loop and follow the execution manually using scratch paper if you need to.

Play this video for an example of this:

Video: manually stepping though your code

Question:
Now, use the technique to step thought this loop:

$x = 2;
$y = 1;
while( $x < 20 ) {
echo $x;
$y = $y + 1;
$x = $x * 2 + $y;
}
What are the sequence of numbers that this loop will echo?

- 2( |,)+6( |,)+15( )*
Feedback: correct

do..while and for loops

read about the do..while and the for loop on pages 119 to 123.

Note:

The best way to understand the for loop is to memorize this flowchart for it:

The most common for loop is just to count from 1 or 0 up to some number:

This flow chart counts from 0 to 10:

for( $counter = 0; $counter <= 10; $counter++) {
echo $counter;
}
Try writing each part of the for loop into the flow chart and then see if you can follow step by step the operation of the loop by following the code and the flow chart. It will echo 0 1 2 3 4 5 6 7 8 9 10

Question:
What statement can you put inside a loop to cause it to exit the loop immediately.

- (break|BREAK)(|;)

nesting control structures and spreading control structure between code blocks

Read 124 till the end of the chapter.

note on page 124 it talks about the backslash character in a string with double quotes. The use of backslash in combination with the character following it cause special characters to be put into the string that you would not be able to otherwise. Here is a list of the most useful ones:

backslash code | character inserted
\\ | \
\" | "
\n | a new line

The technique of returning to HTML mode inside a loop of if statement is very valuable.

Listing 6.12 can be rewritten using this technique like this:

<table style="border: 1px solid black;">
<?
for($y = 1; $y <= 12; $y++) { ?>
<tr><?
for( $x = 1; $x <= 12; $x++) { ?>
<td style="border: 1px solid black; width: 25px; padding: 4px; text-align: center"> <?
echo ( $x * $y ); ?>
</td> <?
} ?>
</tr> <?
}
?>
</table>

This eliminates all the echo statements with HTML code in them.

Question:

The \n puts in a new line character into a string for output, what advantage doe this have if you use it in echo statements?

- It is of no real use.
- It can make the source HTML code output easier to read
- it is required by browsers
- You need it to start a new paragraph