In this article, we will figure out how we can utilize together with the different contingent proclamations and loops in PHP programming language with examples?
As referenced before, the looping articulation is executing a specific code up to a condition is valid. On the request hand, restrictive articulations are explanations that must be executed dependent on the satisfaction of a specific condition(s).
Example:
We have 3 telephones and 3 PCs inside and out 6 devices. How about we compose a fundamental program that presentations “telephone” multiple times and “PC” multiple times expecting the telephones are named 1 to 3 and the PCs 4 to 6.
We are going to perceive how we can utilize the restrictive articulations and the loop explanations to achieve this. Since we have had a dominance of the syntaxes structure the past articles we will go directly to their execution.
Utilizing for loop and if…else
<?php
for ($l = 1;$l <= 6;$l++) {
if ($l <= 3) {
echo "<br>phone";
} else {
echo "<br>laptop";
}
}
?>
Output:
phone
phone
phone
laptop
laptop
laptop
Utilizing while loop and if…else
<?php
$x = 1;
while ($x <= 6) {
if ($x <= 3) {
echo "<br>phone";
} else {
echo "<br>laptop";
}
$x++;
}
?>
Output:
phone
phone
phone
laptop
laptop
laptop
Utilizing do while loop and if…else
<?php
$x = 1;
do {
if ($x <= 3) {
echo "<br>phone";
} else {
echo "<br>laptop";
}
$x++;
} while ($x <= 6);
?>
Output:
phone
phone
phone
laptop
laptop
laptop