Php While Loops

The while loop executes a block of code as long as the specified condition is true.

Syntax :-

Example :-

Result

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

PHP do...while Loop

The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

Syntax :-

Example :-

Result

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time.