While loop

The while loop - Loops through a block of code as long as the specified condition is true.

 

PHP While Loop

If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

Syntax:

while (condition is true) {
  code to be executed;
}

 

Example:

";
    $x++;
  } 
?>

 

Output:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

 

Example Explained

  • $x = 1; - Initialize the loop counter ($x), and set the start value to 1
  • $x <= 10 - Continue the loop as long as $x is less than or equal to 10
  • $x++; - Increase the loop counter value by 1 for each iteration