This time we will discuss looping in php. Iteration in php has its own characteristics. Regularly when you compose code, you need a similar square of code to run again and again a specific number of times. Thus, rather than including a few practically equivalent code-lines in a content, we can utilize circles.
Circles are utilized to execute a similar square of code over and over, up to a specific condition is valid.
In PHP, we have the accompanying circle types:
- while – circles through a square of code as long as the predetermined condition is valid
- do…while – circles through a square of code once, and afterward rehashes the circle as long as the predetermined condition is valid
- for – circles through a square of code a predetermined number of times
- foreach – circles through a square of code for every component in an exhibit
The accompanying sections will clarify and give instances of each circle type.
PHP While Loop
The while loop – Loops through a square of code as long as the predetermined condition is valid. See the example below.
The example below displays the numbers from 1 to 5:
1 2 3 4 5 6 7 |
<?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> |
Example Explained
- $x = 1; – Initialize the loop counter ($x), and set the start value to 1
- $x <= 5 – Continue the loop as long as $x is less than or equal to 5
- $x++; – Increase the loop counter value by 1 for each iteration
This example counts to 100 by tens:
1 2 3 4 5 6 7 |
<?php $x = 0; while($x <= 100) { echo "The number is: $x <br>"; $x+=10; } ?> |
The PHP Do While Loop
The do…while loop will consistently execute the square of code once, it will at that point check the condition, and rehash the circle while the predefined condition is valid.
See the example below. The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:
1 2 3 4 5 6 7 |
<?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> |
Note: In a do…while loop the condition is tested AFTER executing the statements within the loop. This means that the do…while loop will execute its statements at least once, even if the condition is false. See example below.
This example sets the $x variable to 6, then it runs the loop, and then the condition is checked:
1 2 3 4 5 6 7 |
<?php $x = 6; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> |
The PHP For Loop
The PHP for loop is utilized when you know ahead of time how often the content should run.
Parameters:
- init counter: Initialize the loop counter value
- test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
- increment counter: Increases the loop counter value
See the example below. The example below displays the numbers from 0 to 10:
1 2 3 4 5 |
<?php for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?> |
Example Explained
- $x = 0; – Initialize the loop counter ($x), and set the start value to 0
- $x <= 10; – Continue the loop as long as $x is less than or equal to 10
- $x++ – Increase the loop counter value by 1 for each iteration
See the example below. This example counts to 100 by tens:
1 2 3 4 5 |
<?php for ($x = 0; $x <= 100; $x+=10) { echo "The number is: $x <br>"; } ?> |
The PHP Foreach Loop
The foreach circle works just on exhibits, and is utilized to circle through each key/esteem pair in a cluster.
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.
See the example below. The following example will output the values of the given array ($colors):
1 2 3 4 5 6 |
<?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?> |
The following example will output both the keys and the values of the given array ($age):
1 2 3 4 5 6 |
<?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); foreach($age as $x => $val) { echo "$x = $val<br>"; } ?> |