PHP Free Tutorial

Loops in PHP

In PHP, Programming by Baqir Ali

As the name suggests, loops are used to loop (or iterate) over code blocks. The following section shows the syntax for different types of loops. Each loop will return “12345”.

While Loop in PHP

while loops are used to execute a  block of code repeatedly while one or more conditions are true.

$a=1;
while ($a < 6)
{
     echo $a;
     $a++;
}

Do While Loop in PHP

Do-while loops are used to execute a block of code repeatedly until one or more conditions are found to be false. The difference between while loops and do…while loops are that the condition is checked after the code block is executed. This means that, in a do-while loop, the code block will always be executed at least once.

$a=1; do
{
      echo $a;
      $a++;
}while ($a < 6);

For Loop in PHP

A for loop takes three expressions separated by semi-colons and grouped in parentheses before the block to be iterated through.

  1. Loop Initialization: The first expression is executed once before the loop starts. It is usually used to initialize the conditions.
  2. Loop Condition: The second expression is evaluated before each iteration through the loop. If it evaluates to false, the loop ends.
  3. Loop Increment or Decrement: The third expression is executed at the end of each iteration through the loop. It is used to bring changes inside the counter variable.
Syntax

for (Initialization; Condition; Inc/Dec)
{
      //Code
}

Example 1: Actual Code to produce 12345

for ($a=1; $a < 6; $a++)
{
      echo $a;
}

Example 2: Find either the values of the counter variable is odd or even in PHP

<?php
//ODD Or EVEN
for ($i=1; $i <=10 ; $i++) { 
			$rem=$i%2;
			if($rem==0){
				echo $i." IS EVEN.";
			}
			else{
				echo $i." IS ODD";
			}

			echo "<br>";
		}
?>

Output:

1 IS ODD
2 IS EVEN.
3 IS ODD
4 IS EVEN.
5 IS ODD
6 IS EVEN.
7 IS ODD
8 IS EVEN.
9 IS ODD
10 IS EVEN.

Example 3: Finding sum and average of the counter variable in PHP.

<?php
#Finding average
		$sum=0;
		for ($i=1; $i <=10 ; $i++) { 
			$sum=$sum+$i;
		}
		echo "Sum is ".$sum;
		$ave=$sum/10;
		echo "<br>Average is ".$ave;
?>

Output:

Sum is 55
Average is 5.5

Example 4: Finding Prime Numbers in PHP

<?php
#finding prime numbers
	$num=18;
	$count=0;
	for ($i=1; $i<=$num ; $i++) { 
		if($num%$i==0)
			$count++;		
	}
	if($count<=2)
	{
		echo "PRIME";
	}
	else
	{
		echo "Composite";
	}
?>

Output:

Composite

Example 5: Finding Sum odd and even numbers in PHP

<?php
#sum odd and even

	$sum=0;
	$even=0;
	for ($i=1; $i <=20; $i++) { 
		if($i%2!=0)
			$sum=$sum+$i;
		else
			$even=$even+$i;
	}
	echo "Total Odd is ".$sum;
	echo "Total EVEN is ".$even;
?>

Output:

Total Odd is 100 Total EVEN is 110

Break and Continue Statement in PHP

  • Break: It is used to break out of the loop
  • Continue: It cancels all the statements after and continues the loop beginning.
for ($a=1; $a < 6; $a++)
{
     echo $a;
     if ($a > 3)
     {
          break;
     }
}

To jump to the next iteration of a loop without executing the remaining statements in the block, insert a continue statement.

for ($a=1; $a < 6; $a++)
{
     if ($a == 3)
     {
          continue;
     }
     echo $a;
}

Foreach Loop in PHP

The for-each loop is designed to work with arrays and works only with arrays. Arrays are covered later, but because we are talking about looping constructs, the for-each loop should be mentioned here.

An array is a list of items, like an array of numbers or strings. The loop expression consists of the array name, followed by the keyword, and a user-defined variable that will hold each successive value of the array as the loop iterates. The for-each loop, as the name implies, works on each element of the array, in turn, moving from left to right, until all of the elements of the array have been processed. The loop expression is followed by a block of statements that will be executed for each item in the expression.

Syntax
$array_name=array( item1, item2, item3, ...); foreach ($array_name as $value)
{
     do-something with the element's value;
}

Foreach Loop Code Example in PHP

<?php
	$names=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	foreach ($names as $day) {
		echo "$day <br>";
	}
?>

Output:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Support us by sharing this post