TehByte
Active Members
The while loop executes a block of code while a condition is true.
In this code, while the variable $i is less than 3 the while loop shall run, and the output shall be:
The for loop is used when you know how many times your script should run.
This example defines a loop with the variable $i=0. The loop shall continue to run as long as $i is less than or equal to 5.
Output:
Sources: w3schools.com
Code:
<?php
$i=0;
while($i < 3) {
echo "Your number is: " .$i. "<br />";
$i++;
}
?>
Code:
Your number is 0
Your number is 1
Your number is 2
Your number is 3
Code:
<?php
for($i=0; $i<=5; $i++) {
echo "Your number is: " .$i. "<br />";
$i++;
}
?>
Output:
Code:
Your number is 0
Your number is 1
Your number is 2
Your number is 3
Your number is 4
Your number is 5
Last edited by a moderator: