Loop Concept on PHP

loops in php

Welcome back, developers! Today I want to share detailed concepts of loops on PHP. The topics we cover in these articles are as follows

  1. What is loops
  2. Types of Loops
  3. How to use loops and when to use loops
  4. Examples of loops

What is Loops in PHP?

Loops are executing some block code based on conditions. In PHP loops means executing the same block code a specific number of times.

PHP supports four types of loops. As the name indicates loops are iterations of some conditions for specific numbers.

Loops are used to execute repeatedly some block of code for specific statements and for specific of conditions.

Types of loops

There are four types of loops. Each type of loop has its syntax.

Those are the following

  1. For Loop
  2. While Loop
  3. Do While Loop
  4. Foreach Loops

For Loop

For loop is used to execute some block of code for a specific number of times. For loops, we must know the start point and the ending point numbers.

Syntax of for loops

<?PHP 
for($i =0; $i < 10; $++){ echo $i;}
?>

The above code shows

$i= is a variable that iterete from 1 to 10;

$i < 10 checks the conditions of the block less than 10

$++ = Increaments the number by 1.

echo = display the result for a specific condition is true;

Examples of For Loops

<?php
for($i =0 ; $i < 10; $i++){
echo $i .'<br>';
}
 ?>

The above code displays numbers from 0 to 9.

At first, check if it displays 0, and second it displays 1 ….. at 10th check it displays 9.

Outputs

the above image shows the number from 0 to 9

Nested For Loops

Nested for loop means a for loop inside a for loop. We can use for different purposes nested for loops. To show the patterns, to control many to many relationships, one to one relationship.

Nested loops are used displays to arrays at the same time. By using nested for loops we created 2D arrays and 2D applications.

nested for loo examples by image

<?php 
echo "<h2>Example 1 </h2>";
for ($i=0; $i < 5; $i++) 
{ 
    for ($j=0; $j < 5; $j++)
     { 
        echo "*";
    }
    echo "<br>";
}
echo "==================";
echo "<h2>Example 2 </h2>";
for ($i=0; $i < 5; $i++) 
{ 
    for ($j=0; $j < $i; $j++)
     { 
        echo "*";
    }
    echo "<br>";
}
echo "==================<br>";
echo "<h2>Example 3 </h2>";
for ($i=1; $i < 5 ; $i++) { 
    for ($j=5; $j > $i; $j--) { 
        echo "*";
    }
    echo "<br>";
}
?>

The above code shows the pattern of different forms

Outputs

the above image shows the outputs the codes.

2. While Loops

While loop executes the block of condition as long as the condition is true. If the condition is not true the while loop stop. Does not execute any functions.

While loop executes the block of code for an unknown number until the condition is true.

Syntax of While Loops

While(the condition to be true){

//execute the conditions

}

Examples of While loops

<?PHP
$i=1;
while($i < 8){
echo $i;
$i++;
}
 ?>

As the above code shows

At first time we must declare the variable $i=1;

While($I < 8) conditions of the block, it executes until the condition gets false.

$i++; show the increase in our values by one if the condition is true.

Example 2 of While Loops

Create a PHP program that displays even and odd numbers by using while loops.

To create Even and Odd numbers we have to check the number in the while condition if modulo (%) of two zero or not.

If Number%2= zero the number is even else the number is odd.

So let’s start coding now here…

<?php
$even= "";
$odd= "";
$i=1;


while($i < 21){
	if($i % 2 ==0){
		$even=$even ." ".$i;
	}
	else{
		$odd= $odd ." ".$i;
	}

	$i++;
}
echo "Even = ".$even .'<br>';
echo "Odd = ".$odd;

?>

Outputs

As we show in the above image even and odd numbers from 1 to 20

Do While Loops in PHP

Do While loop executes the block of code once and loops through the block until the condition is true.

Do while loop is a control flow statement, in the do while loop first loops the block and then checks the conditions. When checking the condition the block may be false it kill the loops.

Syntax of Do While Loops

<?PHP

do {

execute the block and check

}

while(conditions)

?>

Examples of Do while loops

By using do while loop generate number from 0 to 10.

<?php 
$x=1;
do{
   echo $x .'<br>';
      $x++;
}
while($x <= 10);
?>

The main difference while and do while loop

While loop

  • is also called entry controls
  • if the condition is false, the block of code does not executes
  • Condition check first and then execute block
  • does not require a semicolon to exit executions

Do while loop

  • It is a control exit.
  • The block of code executes first and then checks the condition
  • semicolon is required to exit conditions

4. Foreach Loops

Foreach loops construct the easest way to loops arrays of the elements.

Foreach loop allocate temporary memory locations for index values.

Syntax of Foreach loops

<?PHP

foreach($values as $data){

//execute block of codes until the data

}

?>

foreach loops used for iterating array of data that comes from database of from other .

example of Foreach loops

Display arrays of data that have the following attributes

<?php
$colors=array('green','red','white','black');
foreach ($colors as $key => $color)
 {
	echo $color ;
}
?>

The above code display the result of arrays

as follow

Summarry

As summary we have cover all types of loops that included in PHP.

Types of loops

  1. For loop
  2. While Loop
  3. do while loops
  4. foreach loops

Author: bbnadmin

Leave a Reply

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