PHP Tutorials
PHP Forms
PHP Advanced
PHP OOP
Usually when you write code, you want the same code block to run repeatedly a certain number of times. So, instead of adding a few lines of code that almost match the text, we can use loops.
Loops are used to use the same block of code many times, as long as the particular situation is true.
In PHP, we have the following loop types:
while
- loops through a block of code as long as the specified condition is truedo...while
- loops through a block of code once, and then repeats the loop as long as the specified condition is truefor
- loops through a block of code a specified number of timesforeach
- loops through a block of code for each element in an arrayWhile loop - Connects to the code block as long as the specified condition is true.
Loop while using the code block as long as the condition stated is true.
while (condition is true) {
code to be executed;
}
The example below shows the numbers from 1 to 5:
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
This example counts 100 in tens:
<?php
$x = 0;
while($x <= 100) {
echo "The number is: $x <br>";
$x+=10;
}
?>
Do...while loop
- It goes into the code block once, and then repeats the loop as long as the specified condition is true.
The do...while
the loop will always use the code block once, then check the status, and repeat the loop when the specified state is true.
do
{
code to be executed;
}
while (condition is true);
The example below starts with setting the variable x x 1 ($ x = 1). Then, loop the do while it will record another output, and then raise the variable $ x per 1. Then the condition is checked ($ x less, or equal to 5?), And the loop will continue to work as long as $ x is less, or equal to 5:
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Note: In practice do... while
loop status is checked AFTER using statements within the loop. This means that do...
while the loop will use its statements at least once, even if the situation is false. See an example below.
This example sets a variable of x x to 6, then uses a loop, and then the status is checked:
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
For Loop - A loop in a code block a certain number of times.
The loop is used if you know in advance how often the script should work.
for (init counter; test counter; increment counter)
{
code to be executed
for each iteration;
}
The example below shows numbers from 0 to 10:
<?php
for ($x = 0; $x <= 10; $x++)
{
echo "The number is: $x <br>";
}
?>
This example counts 100 in tens:
<?php
for ($x = 0; $x <= 100; $x+=10)
{
echo "The number is: $x <br>";
}
?>
For-loop
- Connects to each element code block to the same members.
The forloop
only applies to the same members, and is used to connect between each key / value pairing in the system.
foreach ($array as $value)
{
code to be executed;
}
In each loop of the loop, the current element of the same members is assigned to the value of $ and the identifier of the same members is moved one by one, until it reaches the last half of the same members.
The following example will give you the colors of the same given members ($ colors):
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
The following example will extract both the keys and the values of the same given member ($ age):
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $val) {
echo "$x = $val<br>";
}
?>
You have already seen the break
statement used in the previous chapter of this study. Used for "skipping" on the switch
statement.
The break
statement can also be used to jump into a loop.
This pattern comes out of the loop when x is equal to 4:
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
The continue
statement terminates one replica (loop), if the specified condition occurs, and continues the following repetition in loop.
This example exceeds 4 value:
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
You can also use break
and continue while
loops:
<?php
$x = 0;
while($x < 10) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
$x++;
}
?>
<?php
$x = 0;
while($x < 10) {
if ($x == 4) {
$x++;
continue;
}
echo "The number
is: $x <br>";
$x++;
}
?>