Loops
Loops
While loop
while loops are the simplest type of loop in PHP.
The basic form of a while statement is:
while (condition)
statement
While loop continued….
The value of the expression is checked each time
at the beginning of the loop, so even if this value
changes during the execution of the nested
statement(s), execution will not stop until the end
of the iteration (each time PHP runs the
statements in the loop is one iteration).
For example
<
? php
$i = 1;
while ($i <= 10) {
echo $i++;
}
?>
Try it out
<?php
$i = 0
while ($i < 3)
{
$i++;
}
print $i;
?>c) 0
d) 1
a) 2
b) 3
Correct answer
a) 2
b) 3
c) 0
d) 1
Explanation: The increment happens and then
the check happens
<?php
$i = 0
while ($i++)
{
print $i;
}
print $i;
?>
a) 0
b) infinite loop
c) 01
d) 1
Correct answer
a) 0
b) infinite loop
c) 01
d) 1
Explanation:As it is a post increment, it checks
and then does not enter the loop, thus prints
only 1
Do-while