I read this Manual by PHP.com about While loops.
I don’t understand the purpose of While loops in PHP.
It looks exactly like an if statement to me.
What is the difference between an if statement and a while loop?
How do while loops work, what do they do, and when should I use them?
For example, can’t this:
$i = 1;
while ($i <= 10) {
echo $i++;
}
be done like this?:
$i = 1;
if ($i <= 10) {
echo $i++;
}
An
ifstatement checks if an expression is true or false, and then runs the following code block only if it is true. The code inside the block is only run once…A
whilestatement is a loop. Basically, it continues to execute the code in the following block for however long the expression is true.When to use a while loop:
While loops are best used when you don’t know exactly how many times you may have to loop through a condition – if you know exactly how many times you want to test a condition (e.g. 10), then you’d use a for loop instead.