Using While Loops in PHP

An introduction to using while and do-while loops in PHP to perform repetitive tasks efficiently.

Using While Loops in PHP

The difference between computers and humans is that computers can be told to perform the same task repeatedly without ever getting tired. To give such commands to a computer, you can use loops.

The While Loop

While Loop Script Example of a while loop script

When executed, the while loop above produces the following repetition:

While Loop Output Result of the while loop script

The Do While Loop

Do while is another technique for creating loops, with the main difference being that the conditional statement is placed at the bottom. If the previous code is converted to a do while loop, it looks like this:

Do While Script Do while script

The output will appear as follows:

Do While Output Do while used for repetition

Advertisement
ProgrammingPHP
Building a product that has to stay reliable? I help founders and product teams scope, build and scale web and mobile products, with testing built in from day one.
Book a call

Keep reading

All articles