May 11, 2021 PHP
Loop the block of code a specified number of times, or when the specified condition is true.
When you write code, you often need to have the same block of code run over and over again. We can use loop statements in our code to accomplish this task.
In PHP, the following circular statements are provided:
The while loop repeats the block of code until the specified condition is not established.
The following example first sets the value of variable i to 1 ($i is 1;).
Then, as long as i is less than or equal to 5, the while loop continues to run. Each time the loop runs, i increments by 1:
Output:
do... The while statement executes the code at least once and then checks the condition, repeating the loop as long as the condition is in place.
The following example first sets the value of variable i to 1 ($i is 1;).
Then, start do... W hile loop. T he loop increases the value of variable i by 1 and outputs. Check the condition (i is less than or equal to 5) and the loop continues to run as long as i is less than or equal to 5:
Output:
That's all you know about phP while loops, and in the next section, continue to understand for loops and foreach loops.