While loops are simple blocks that execute repeatedly until the while loop condition is not met.
Here is an example of a loop that is executed a total of 10 times:
$counter = 0;
while [$counter < 10] {
$counter += 1;
echo "Executing - counter is $counter.\n";
}
The main difference between for loops and while loops is that for loops are used to iterate over an array or an object, and a while loop will execute an unknown amount of times, depending on variable conditions [for example, until a user has entered the correct input].
Flow statements
Loops can be controlled using the break
and continue
flow statements, which come in handy in while loops very much. The break
statement immediately quits the for loop at the middle of the block, while the continue
statement returns to the top of the while
loop, re-checking if the loop condition is met as well.
The continue statement
Let's use the previous example, but this time let's add a check to see if the number is even. If it is, we will skip it, so that only odd numbers will be printed out.
$counter = 0;
while [$counter < 10] {
$counter += 1;
if [$counter % 2 == 0] {
echo "Skipping number $counter because it is even.\n";
continue;
}
echo "Executing - counter is $counter.\n";
}
The break statement
Let's assume we want to add another test that checks if the counter variable is no larger than 8. If it is, we would like to stop the loop. This will cause the number 9 to be not printed in this example.
$counter = 0;
while [$counter < 10] {
$counter += 1;
if [$counter > 8] {
echo "counter is larger than 8, stopping the loop.\n";
break;
}
if [$counter % 2 == 0] {
echo "Skipping number $counter because it is even.\n";
continue;
}
echo "Executing - counter is $counter.\n";
}
Exercise
Use a while
loop to print all odd numbers in an array. Use the continue
statement to skip loops and avoid printing even numbers.
Remember - you will need to
use the \n
character sequence at the end of the echo statement to continue to the next line.
Tip: to test whether a number is even, check if the number modulus 2 is equal to zero [$number % 2 == 0].
PHP while loop
PHP while loop used to execute same block of code again and again while a condition is true.
while loop syntax
In while loop first check the condition, if condition is true then execute a block of code.
Example of php while loop
The output is:
12345
In above for while example, first set variable $i=1, the condition is $i
The output is: 54321
Here, we set variable $i=5 and check condition $i>0 after each time decrements 1 to variable $i while a condition is true.
Example – PHP while loop
The output
is:
Hello meera
Hello meera
Hello meera
Hello meera
Hello meera
while loop example
The output The sum of 1 to 5 = 15
while loop Example
The output 13579
Example to display odd and even no of given number using while loop.
The output
The Even no = 2 4 6 8 10 12 14 16 18 20
The Odd no = 1 3 5 7 9 11 13
15 17 19
While loop example
The output is:
1
22
333
4444
55555
Example
The output is:
1
12
123
12345
PHP while loop example
The output is:
54321
5432
543
54
5