PHP - Loops

PHP - Loops

  • Loops are used to execute the same block of code again and again, as long as a certain condition is true.

Types of loops:

  • for
  • while
  • do-while
  • foreach

Important features of loops

  • continue
  • break (you've already seen this used with switch)

PHP - The for Statement

  • Use this type of loop when you know how many times you want it to run.

Example 1


<?php
for ($x = 1; $x <= 10; $x++) {
echo "The SoftHubSolution <br>";
}

/* Output:
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
*/

?>
  • The first expression (initialization) is evaluated (run) once unconditionally at the beginning of the loop.
  • At the start of each loop (or iteration), condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are run. if it evaluates FALSE, the execution of loop ends.
  • Atv the end of each iteration, the post-op condition, in this example an increment is evaluated (run).
  • Each of the expression can be empty or contain multiple expressions separated by commas.

PHP - The while Statement

  • Use this type of loop when you want the condition to be tested before the commands are run.

Example 2


<?php
$i=1;
while ( $i <= 5 ) {
echo "The SoftHubSolution <br>";
$i++;
}

/* Output:
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
*/


?>
  • At the start of each loop (or iteration), condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are run. If it evaluates to FALSE, the the execution of the loop ends. So this statement might never run.
  • The big difference between for and while is for runs first, then tests and while tests first, then if the condition is true, then it runs.
  • At the end of each iteration, increment is evaluated (run).

PHP - The do..while Statement

  • A variation of a while statement, do..while allows you to run the code once, then check the condition

Example 3


<?php
$i=1;
do
{
$i++;
echo "The SoftHubSolution <br>";
} while ( $i <= 5 );

/* Output:
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
The SoftHubSolution
*/


?>
  • At the start of each loop (or iteration), condition is evaluated. If it evaluates to TRUE, the loop continues and the neseted statement(s) are run. If it evaluates to FALSE, the execution of loop ends. This statements is always run at least once, but may not repeat depending on the ending conditional in the while statement.
  • At the end of each iteration, increment is evaluated (run).

PHP - foreach Statement

  • foreach works on arrays and is used to loop through each key/value pair in an array.

Example 4


<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
echo "$value <br>";
}

/* Output:
red
green
blue
yellow
*/


$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
echo "$x = $val<br>";
}

/* Output:
Peter = 35
Ben = 37
Joe = 43
*/

?>

PHP - continue statement

  • continue is used within looping structures (do, while, for, foreach) to skip the rest of the current loop iteration and go to the beginning of the next evaluation.
<?php
for ($i=0; $i<=5; $i++) {
if($i==3) {
continue;
}
echo "The number is ".$i;
echo "<br/>";
}

/* Output:
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
*/


?>

PHP - Break statement

  • Break ends running of the current for, foreach, while, do-while or switch structure.
<?php
for ($i=0; $i<=5; $i++) {
if($i==3) {
break;
}
echo "The number is ".$i;
echo "<br/>";
}

/* Output:
The number is 0
The number is 1
The number is 2
*/


?>

Post a Comment

Previous Post Next Post