We now have a youtube channel. Subscribe!

Loops in PHP

PHP Loops


Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we will be discussing about the PHP Loops.

Loops are used to execute the same block of code a specified number of times. PHP supports the following four types of loops -

  • for loop - loops through a block of code in a specified number of times.
  • while loop - loops through a block of code if and as long as the specified condition is true.
  • do...while loop - loops through a block of code once, and then repeat the loop as long as a special condition is true.
  • foreach loop - loops through a block of code for each of the element in an array.

In this tutorial, we will discuss about break and continue keywords used to controll the loops execution.


PHP For Loop Statement

The for loop statement

The For loop statement is used when you know how many times you want to execute a statement or a block of statements.

Flowchart

PHP For Loop Flowchart

Syntax

for (initialization; condition; increment){
   code to be executed;
}

The initializer is used for setting the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and and it is traditional to name it $i.

Example

The following example below makes five iterations and changes the assigned value of two variables on each pass of the loop -

<html>
   <body>
      
      <?php
         $a = 0;
         $b = 0;
         
         for( $i = 0; $i<5; $i++ ) {
            $a += 10;
            $b += 5;
         }
         
         echo ("At the end of the loop a = $a and b = $b" );
      ?>
   
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

At the end of the loop a = 50 and b = 25


PHP While Loop Statement

The while loop statement

The PHP while loop statement will execute a code or block of code if and as long as a test expression is true.

If the test expression is true, then the code block will be executed. After the code has executed, the test expression will again be evaluated and the loop is going to continue until test expression is found to be false.

Flowchart

PHP While Loop Flowchart

Syntax

while (condition) {
   code to be executed;
}

Example

The following example below decrements a variable value on each iteration of the loop and the counter increments untill it reaches 10 when the evaluation is false and the loop ends.

<html>
   <body>
   
      <?php
         $i = 0;
         $num = 50;
         
         while( $i < 10) {
            $num--;
            $i++;
         }
         
         echo ("Loop stopped at i = $i and num = $num" );
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Loop stopped at i = 10 and num = 40 


PHP Do While Loop Statement

The do...while loop statement

The do....while loop statement is going to execute a block of code at least once. It will then repeat the loop as long as a condition is true.

Syntax

do {
   code to be executed;
}
while (condition);

Example

The example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value less than 10 -

<html>
   <body>
   
      <?php
         $i = 0;
         $num = 0;
         
         do {
            $i++;
         }
         
         while( $i < 10 );
         echo ("Loop stopped at i = $i" );
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Loop stopped at i = 10


PHP Foreach Loop Statement

The foreach loop statement

Foreach loop statement is used for looping through arrays. For each pass, the current array element's value is assigned to $value and the array pointer is moved by one and in the next pass, the next element is going to be processed.

Syntax

foreach (array as value) {
   code to be executed;
}

Example

Try the following example to list out the values of an array.

<html>
   <body>
   
      <?php
         $array = array( 1, 2, 3, 4, 5);
         
         foreach( $array as $value ) {
            echo "Value is $value <br />";
         }
      ?>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5


PHP Break Statement

The break statement

The break keyword is used to terminate the execution of a loop prematurely.

The PHP break statement is situated inside the statement block. It gives you full control of the loop and whenever you want to come out from the loop, you can come out. After exiting a loop, the immediate statement to the loop will be executed.

Flowchart

PHP Break Statement Flowchart

Example

In the following example, the condition test becomes true when the counter value gets to 3 and the loop terminates.

<html>
   <body>
   
      <?php
         $i = 0;
         
         while( $i < 10) {
            $i++;
            if( $i == 3 )break;
         }
         echo ("Loop stopped at i = $i" );
      ?>
   
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Loop stopped at i = 3


PHP Continue Statement

The continue statement

The PHP continue statement is used to halt the current iteration of a loop but it doesn't terminate the loop.

Just like the break statement, the continue statement is located in the statement block containing the code that the loop executes, followed by a conditional test. For the pass facing continue statement, rest of the loop code is skipped and next pass starts.

Flowchart

PHP Continue Statement Flowchart

Example

In the following example below, loop prints the value of an array but for which condition becomes true it skips the code and the next value is printed.

<html>
   <body>
   
      <?php
         $array = array( 1, 2, 3, 4, 5);
         
         foreach( $array as $value ) {
            if( $value == 3 )continue;
            echo "Value is $value <br />";
         }
      ?>
   
   </body>
</html>

Output

When the above code is executed, it will produce the following result -

Value is 1
Value is 2
Value is 4
Value is 5


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Arrays.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain