Loops

So far we've looked at decisions that a script can make about what code to execute. Scripts can also decide how many times to execute a block of code. Loop statements are designed to enable you to achieve repetitive tasks. A loop will continue to operate until a condition is achieved, or you explicitly choose to exit the loop.

The while Statement

The while statement looks similar in structure to a basic if statement:

while (expression) {
      // do something
}

However, unlike an if statement, a while statement will execute for as long as the expression evaluates to true, over and over again if need be. Each execution of a code block within a loop is called an iteration. Within the block, you usually change something that affects the while ...

Get Sams Teach Yourself PHP, MySQL® and Apache All in One now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.