How to create a while loop in PHP

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to create a while loop in PHP

In this post snipped I’ll show you an easy way to create simple while loops and multiple or nested while loops.

Scenario

Create a counting script that stops once it reaches 10 and it also prints the count to the user once the execution has finished.

Solution

Output

Multiple while loops in PHP

Heads up! Don’t forget that nesting loops within another loop will have the main loop hang (wait) until the nested one is completed. 

Output

That’s it with php while loops, from here you can take another step further and think it with arrays for example.

Request an article ←