PHP while loop
In this tutorial I will show you how with code examples to use while loops in PHP.
Step 1 - The while loop
PHP while loop
If you want to repeat the execution of a code block you can use loops in any programming languages. In PHP there are more ways to do it. First we will discuss the while loop, which is maybe the simplest loop type in PHP. The loops in general check their condition and depends on the result the code block will be executed 1 or more times or will not be executed at all. So the definition of the while loop is a bit similar to the definition of the if statement. It looks like this:
while (condition) statementAnd a real example looks like this:
Code:
while ($x < 10) $x++;
The while loop first checks the condition and if it is true then execute the relevant code block. After the code was executed the while loop checks the condition again and executes the code again if neccessary. This will be repeated until the condition is true. So you have to take care not to make a never ending loop like this:
In this case the x variable is never incremented so the condition will be always true. This results an endless loop.
Next Step of PHP while loop
Tags: php while loop, while loop, php while, php, while, loop
| PHP while loop - Table of contents |
|---|
| Step 1 - The while loop |
| Step 2 - Break and continue in the while loop |
