PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Loops

A loop is a sequence of instructions that is repeated while a certain condition is true.

PHP Loops

In PHP, there are 4 types of loops.

Loop Description
while Executes a block of code repeatedly while a certain condition is true
do-while Works exactly same as while.
But, in the first loop, the block of code is executed even the condition is true or false.
for Loops through a block of code a specific number of times.
foreach Loops over arrays.

PHP While Loop

While loops are the simplest loops in PHP.

While Loop - Syntax

while (condition) { code to execute if the condition is true }
PHP While Loop

In the example below,

  • First, the variable $a is set to 1.
  • Then, while statement will execute the code inside it repeatedly while $a is less than 10.
  • $a will increment by 1 in each execution of the code block.
  • PHP While Example

    $a = 1;
    while ($a < 10) {
    	echo 'Now, $a is ' . $a;
    	echo '<br>'; // line break
    	$a++; // increment by 1
    Run Example ››

    Note: We discussed increment (++) and less than (<) operators in the operators chapter.

    PHP Do-While Loop

    Do-while loops are very similar to while loops, except the condition is checked at the end of the loops instead of in the beginning. So, the block of code of do-while loops is executed at least one time. Thereafter, it will be executed if the condition is true.

    do { block of code to execute once, and then, while the condition is true } while (condition)
    PHP Do-While Loop

    In the following example,

  • First, the variable $a is set to 0.
  • Then, the block of code is executed once.
  • Then, the block of code is executed while $a is larger than 0. (It won't loop because $a is 0)
  • PHP Do-While Example

    $a = 0;
    do {
    	echo 'The number is ' . $a;
    } while ($a > 0);
    Run Example ››

    Note: If you used while loop in the above example, it would not execute the code inside, because the condition is false.

    PHP For Loop

    PHP for loops are little bit complex than the loops we learned previously.

    The syntax of for loops is as follows. In PHP, this is very important!

    for (initial expression, conditional expression, loop-end expression) { block of code to be executed }
    PHP For Loop

    We can use for loop to print numbers from 0 to 10

    PHP For Loop 0 to 10 Example

    for ($a = 0; $a <= 10; $a++) {
    	echo $a . '<br>';
    Run Example ››
    Each of the expressions can be empty or contain multiple expressions separated by commas.

    This example shows you how to use multiple expressions.

    PHP For Loop Multiple Expressions Example

    for ($a = 0, $b = 5;$a <= $b;$a++, $b--) {
    	echo "\$a = $a and \$b = $b";
    	echo '<br>';
    Run Example ››

    Important: Loops can be nested.

    We can draw a square with two nested for loops.

    PHP For Loop Sqauare Example

    <div style="line-height:0.5">
    for ($y = 0; $y < 10; $y++) {
    	for ($x = 0; $x < 10; $x++) {
    		echo '*';
    	echo '<br>';
    Run Example ››

    Note: The line-height property specifies the height of a line. See the PHP in HTML chapter to learn more about using HTML in PHP.

    Foreach loop

    The foreach loop works with arrays. Therefore we will discuss it in PHP Arrays chapter.