×

  PHP

Introduction Getting Started Data Types Operators Conditional Statements Loops Functions Arrays Include Files GET and POST

  Mysql

Data Base Concepts Mysql Talking to Mysql

  PHP & Mysql

Connect DB Mysql insert Mysql select Mysql update Mysql delete
XAMPP Lite S/W

PHP Loops

Scripts can also decide how many times to execute a block of code. Loop statements are specifically designed to enable you to perform repetitive tasks because they continue to operate until a specified condition is achieved or until you explicitly choose to exit the loop.

PHP supports four different types of loops.

• while — loops through a block of code as long as the condition specified evaluates to true.

• do…while — the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.

• for — loops through a block of code until the counter reaches a specified number.

• foreach — loops through a block of code for each element in an array.

The while Statement

The while statement looks similar in structure to a basic if statement, but has the ability to loop:

while (expression) {
// do something
}

Unlike an if statement, a while statement executes for as long as the expression evaluates to true, over and over again if need be. Each execution of a code block within a loop is called an iteration. Within the block, you usually change something that affects the while statement’s expression; otherwise, your loop continues indefinitely. For example, you might use a variable to count the number of iterations and act accordingly.

<?php
$number = 1;
while ($number < 100) {
echo $number . "
"; $number++; } ?>

The do...while Statement

A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it:

do {
// code to be executed
} while (expression);

The test expression of a do...while statement should always end with a semicolon.

This type of statement is useful when you want the code block to be executed at least once, even if the while expression evaluates to false. The code block is executed a minimum of one time.

<?php
$number = 1;
do{
echo $number . "<br>";
$number++;
}while ($number < 100);
?>

The for Statement

Anything you want to do with a forstatement can also be done with a while statement, but a for statement is often a more efficient method of achieving the same effect. In Listing 6.6, you saw how a variable was initialized outside the while statement and then tested within its expression and incremented within the code block. With a forstatement, you can achieve this same series of events, but in a single line of code. This allows for more compact code and makes it less likely that you might forget to increment a counter variable, thereby creating an infinite loop:

for (initialization expression; test expression; modification expression) {
// code to be executed
}


<?php
for ($number =1; $number < 100; $number++){
echo $number . "
"; } ?>

PHP foreach Loop

The foreach loop is used to iterate over arrays.

foreach($array as $value){
    // Code to be executed
} 

<?php
$colors = array("Red", "Green", "Blue");
// Loop through colors array
foreach($colors as $value){
echo $value . "<br>";
?>