PHP

LOOPS IN PHP

A Loop is an Iterative way that involves executing the same number of code a number of times until a certain condition is met or condition is false.

In php their are four types of loops

  1. for loop
  2. while loop
  3. do-while loop
  4. for-each loop

For Loop

The for loop is used when you know in advance how many times the script should run.

Syntax :

for (initialization variable; condition check; increment/decrement )

In for loop syntax their are three parameters

So First parameter INITIALIZATION variable means that initialization of counter values ( variable value) .This means that in this parameter we initialize a variable with a starting value for example $i=0,1,2,3,4……..n

Second Parameter tells about condition apply on your variable i.e when you start your variable at a certain value. so their is also a stop condition or false condition at which you stop.So basically this parameter tells false condition .

Third parameter increase or decrease the variable or counter value after condition is true.

so take an example of for loop how it works

<?php
   for ($x = 0; $x <= 10; $x++) {
      echo "The number is: $x <br>";
   }
?>

Example Explained

  • $x = 0; – Initialize the loop counter ($x), and set the start value to 0.
  • $x <= 10; – Continue the loop as long as $x is less than or equal to 10.
  • $x++ – Increase the loop counter value by 1 for each iteration.
this image tells about how for loop working and what is a flow of three parameter
LOOP-for loop execution control structure

While Loop

In this loop we work on same concept as we read previous.But the Syntax for writing while loop is different than for loop.The while loop takes a condition that decide whether the loop will execute or not.

So Let’s see the Syntax of while loop

while (condition is true) {
    code to be executed;
}

In while condition we pass only a condition on behalf of condition our loop will execute the code inside while loop.

So take an example of while loop

<?php
  $x = 0; 
  while($x <= 100) {
     echo "The number is: $x <br>";
     $x+=10;
  }
?>

Example Explained

So In This example firstly we make an variable $x which are initialize by a value 0 and after that we start while loop in which condition is $x<=100 .this means that the condition is true until the $x value is greater than 100. this loop code print statement 101 times the increment value of $x.

This is a while loop control structure diagram that show how while condition is working
while loop control structure

do-While Loop

In do-while  loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

So Let’s see Syntax of do-while

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

As you see in syntax first we write code then check the condition.So let’s see an Example

<?php
   $x = 6;
   do {
       echo "The number is: $x <br>";
       $x++;
    } while ($x <= 5);
?>

So In above example we see that $x variable initialize with value 6 .so firstly do block statement execute then it increase the $x value by one then it check the condition that $x is less than or equal 5.if condition true then it will execute again do block statement .otherwise it will stop the execution of do block statement and print output to the screen .So In above example statement will print only one time .second time condition will be false.

This image tells the do-while control or flow how the do-while works and it's easy to understand graphically.
do-while loop diagram

For-each Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Let’s see the Syntax

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

In above Syntax $array represent an array.For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

So let’s see an example

<?php
  $names = array("Abhishek", "Shubham", "Shivani", "Adarsh");
  foreach ($names as $value) {
     echo "$value <br>";
  }
?>

So above code print the all array elements by using for-each loop

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button