Search for:


PHP while loops

The while loop allows you to repeat a block of code until the condition is TRUE.

The “while” loop is a control structure in the PHP programming language that allows you to repeatedly execute a block of code as long as a specified condition is true.

The syntax for a “while” loop in PHP is as follows:

while (condition) {
  // code to be executed

In this syntax, “condition” is a Boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. After each iteration, the condition is checked again, and if it is still true, the loop continues. This process repeats until the condition is false, at which point the loop exits and execution continues with the next statement after the loop.

Here is an example of a “while” loop in PHP that prints the numbers from 1 to 10

$num = 1;
while ($num <= 10) {
  echo $num . "<br>";

In this example, the loop condition is $num <= 10, which is true for values of $num from 1 to 10. The code inside the loop simply prints the value of $num and then increments it by 1 using the $num++ statement. The loop continues until $num is no longer less than or equal to 10.