Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP 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 }

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

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

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

    PHP Do-While Loop

    Do-while loops are very similar to while loops, except the condition is checked in 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)

    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

    
    <?php
    $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 loops we learned previously.

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

    for (initial expression, conditional expression, loop-end expression) { block of code to be executed }
    • Initial Expression is executed once unconditionally in the beginning of the loop
    • Conditional Expression is executed in the beginning of each loop. If it returns TRUE, the code block will be executed. If it returns FALSE, the loop will end.
    • Loop-End Expression is executed in the end of each loop.
    Each of the expressions can be empty or contain multiple expressions separated by commas.

    Let's print numbers from 0 to 10

    PHP For Loop 0 to 10 Example

    
    <?php
    for ($a = 0; $a <= 10; $a++) {
    	echo $a . '<br>';
    }
    
    
    Run Example ››

    This example shows you how to use multiple expressions.

    • First, $a is set to 0, and $b is set to 5.
    • The code is executed while $a is less than or equal to $b.
    • After each loop, $a increments by 1, and $b decrements by 1.

    PHP For Loop Multiple Expressions Example

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

    Foreach loop

    Foreach loop works with arrays. Therefore we will discuss about it in PHP Arrays chapter.

    Advertisement
    1
    145