|
|
|
PHP Control Structures II- Looping

- Looping structures run a block of code a specified number of times. If you have code that needs to be executed the same way a number of times use a loop
Looping statements in PHP are used to execute the same block of code a specified number of times.
- In PHP we have the following looping statements:
- while - loops through a block of code if and as long as a specified condition is true
- do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true
- for - loops through a block of code a specified number of times
- foreach - loops through a block of code for each element in an array

The while Statement
The while statement will execute a block of code if and as long as a condition is true.
while (condition)
{
code to be executed;
} |
- The while loop will execute repeatedly as long as the condition is true.
- Initially, we set a counter to a certain value. Before each iteration we test the value of the counter in a condition. As long as it is true, the statements will execute. Then we change the value of the counter.
1 |
$counter = 1; |
2 |
while( $counter <= 10 ){ |
3 |
echo “$counter <br />”; |
4 |
$counter ++; |
5 |
} // end while |
- You can create an endless loop if you do not change the value of the variable
do…while loops
do
{
code to be executed;
}
while( condition ) |
- A do…while loop differs from a while loop because the condition isn’t tested until the statements are executed at least one time. So this means that even if the condition will never evaluate to TRUE, the statements will run one time.
1 |
$counter=0; |
2 |
do |
3 |
{ |
4 |
$counter ++; |
5 |
echo "The number is " . $counter . "<br />"; |
6 |
} |
7 |
while ($counter <5); |

The for Statement
- The for statement is used when you know how many times you want to execute a statement or a list of statements.
for (initialization; condition; increment)
{
code to be executed;
} |
- The for statement has three parameters.
- The three statements within the parentheses must be separated by semicolons.
- The first parameter is for initializing variables
- The condition is tested before each iteration of the loop – if it evaluates as FALSE the loop stops.
- The second parameter holds the condition
- The third parameter contains any increments or decrements required to implement the loop.
- The third parameter is executed at the end of each iteration
- If more than one variable is included in either the initialization or the increment section, then they should be separated by commas.
- The condition must evaluate to true or false.
1 |
for ($i=1; $i<=5; $i++) |
2 |
{ |
3 |
echo "Hello World!<br />"; |
4 |
} |
- Although while and for loops can be used interchangeably, you generally use while when you don’t know how many iterations the loop will make, such as doing something as long as a condition is true.
- A for loop, on the other hand, may be suited better for when you need to run something a certain number of times.
- Two other loops are each( ) and foreach( ) which we’ll look at when we get to array.

Breaking out of a control structure
- If you want to stop executing a loop you can use the break statement as demonstrated in the switch. If you use the break statement in a loop, execution of the script will continue with the next line of script after the loop.
- The exit statement will stop execution of the entire PHP script. It’s often used after performing error checking.

|
|