Loops in PHP with Examples

Loops in PHP with Examples. PHP Loops are a kind of program that can help to run any code inside of the loop to run over and over repeatedly according to our needs as the input and these loops will help execute the code and perform the task infinitely as we want in order to run the same code inside of the loop again and again till the condition becomes true. The word itself tells that it is running to be replicated but only if a specific condition is true that is specified in the loop parameters to check the condition for the PHP loops.

Different types of Loops in PHP

As we know many other programming languages have these loops. They are: While Loop, Do While Loop, For Loop, Foreach Loop. You will get a complete analysis of each and every loop concept of PHP mentioned below.

  • While Loop
  • Do-While Loop
  • For Loop
  • Foreach Loop

While Loop

While Loop will execute the specific block of code which is inside of the while loop brackets of PHP just if the condition specified in the loop is true. If the condition is false the Loop will break the program that is in the continuous process of executing the code.

<?php
$j = 0;
while($j <= 5){
echo " Number is " . $j . "<br>";
$j=$j+1;
}
?>

The above example contains a while loop program in order to print the list of numbers from 0 to 5. Here in the above example, the variable 0 is specified with the number 0, and then the loop code started with the help of $j variable value and the while loop. While Loop began with the condition j<=5 to check whether the $j variable value is less than “5” then the code will be affected that is inside only if the condition is True.

Loop will run repeatedly and prints the values and then the $j value will be incremented by 1 and then breaks the loop when the $j variable value becomes “6” because the condition $j<=5 became false. This code is like printing the natural numbers from 0 to 5 values.

Do While Loop

Do While Loop is just similar to the While Loop but in Do, While Loop condition will not be checked at first although in While Loop condition will be checked first and then the code inside of the loop will execute.

<?php 
$j = 0;

do {
  echo "Number is: $j <br>";
  $j++;
} while ($j <= 5);
?>

The above code contains 1 do while program in order to print the natural numbers from 0 to 5.

For Loops In PHP

For Loop is slightly different while we analyze with the While Loop and the Do While Loop. It runs the code over and over if the particular true condition is satisfied. Loop will execute the code for specific for the number of times as we required and it is controlled by the condition.

For Loop will have three parameters. They are initialization, condition, and the incrementing value inside of the For Loop brackets.

Parameters of the For Loop:

  • Initialization: For Loop, this is the variable in php to start the code.
  • Condition: For Loop, this is the value that is required to be checked. If the condition becomes true then the code of statements will run repeatedly by checking the condition.
  • Incrementing: For Loop, the initial value or executing value in the program statements will be incremented by 1 or other as we required based on our requirement.
<?php
echo "Natural Numbers B/T 1-5: ";
$k=0;
for($j=1; $j<=5; $j++){
echo "$j" . " , ";
$k=$k+$j;
}
echo "<br>";
echo "Sum Natural numbers B/T 1-5: ";
echo "$k";
echo "<br>";
?>

The above for-loop example will print the list of the natural numbers between 1-5 and also the sum of all the values which are between 1-5.

$j is assigned as 1 at first as an initialization value, condition as $j less than or equal to 5 with the incrementation as $jji=$j+1. For Loop will print $j value until the j value becomes 5 and $k variable’s value will store all the numbers/values of $j variable and then it will sum them one by one in the loop until the I value approaches 5. Then the printing of the sum of all the natural numbers between 1-5 will be printed after printing all the natural numbers using the For Loop.

Foreach Loops In PHP

Foreach is one of the loop ideas of PHP which is used to repeat again the array/arrays.

<?php
$Animals = array("Dog", "Elephant", "Sheep", "Goat",);
foreach($Animals as $v){
echo $v . "<br>";
}
?>

The below example will print the values from the $Animals variable. $Animals variable values are the list of the animals. Using the foreach loop concept will print the animals’ names which are present in the array one by one.