Introduction to PHP Loops
PHP Loops are a type of code that can help us to run some code inside of the loop to run over and over again according to our requirement as the input and these loops will help run the code and complete the task infinitely as we want in order to execute the same code inside of the loop again and again until our condition becomes false or else the code runs continuously. The word itself says that it is going to be repeated but only if a certain condition is true which is mentioned in the loop parameters to check the condition for the PHP loop/loops.
Different Loops of PHP
Just like the other programming languages, PHP programming language also offers different types of loop concepts. They are: WHILE LOOP, DO WHILE LOOP, FOR LOOP, FOREACH LOOP. You will get a detailed explanation of each and every loop concept of PHP below.
1. While Loop
While Loop will run the specific/ some block of code which is inside of the while loop parenthesis of PHP only if the condition mentioned in the loop is true. If the condition is false the While Loop will break the code which is in the continuous process of running the code.
Syntax:
While(Condition to check){
//Code which is need to executed or the code statements which is to run
}
Explanation:
In the above syntax, a while loop is mentioned with the condition inside of the parenthesis to run statements inside of the loop only if the condition mentioned is True or else the code inside of the loop will not run by breaking the loop get out of the loop/while loop.
Example:
The below example consists of while loop programming in order to print the list of numbers from 1 to 10. Here in the below example, variable 1 is assigned with the number 1 and then the loop program started with the help of $i variable value and the while loop. While Loop started with the condition i<=10 to check whether $i variable value is less than “10” then code will be executed which is inside only if the condition is True. Loop will run continuously and prints the values and then the $i value will be incremented by 1 and then breaks the loop when the $i variable value becomes “11” because the condition $i<=10 became false. This Program is like printing the natural numbers from 1 to 10 values.
Code:
<?php
$i = 1;
while($i <= 10){
echo " Now The number is " . $i . "<br>";
$i=$i+1;
}
?>
Output:
2. Do While Loop
Do While Loop is just like the While Loop but in Do While Loop condition will not be checked at first whereas in While Loop condition will be checked first and then the programming code inside of the loop will run.
Syntax:
do{
//Programming statements which is need to be executed only if the loop condition is true
}
While(condition to check);
Example:
The below program contains 2 do while programs in order to print the list of even numbers between 1-10 and the list of odd numbers between 1-10. The program also prints the sum of odd numbers, even numbers and also the sum of all the natural numbers which are between 1-10. In the 1st do-while loop the $i variable value is checked whether the value is fully divided by the value “2”. If True then the value will be printed and $k value will store the $i value else nothing happens just the incrementation of the $i variable value. Likewise, the loop continues until the $i value will reach the value “10”. Just like that, others do while loop also runs by checking whether the $j value will not be divided by 2 value or not. If True then $j value will be printed and the $m will store the value. At last, the $k will store the sum of the even numbers and $l will store the sum of odd numbers. $m will store the sum of all the natural numbers and those values will be printed as shown in the pic in the output.
Code:
<?php
$i = 1;
echo "List of Even Numbers between 1-10:: ";
$k = 0;
$m = 0;
do{
if($i%2==0){
echo " $i " ." , ";
$k=$k+$i;
}
$m=$m+$i;
$i=$i+1;
}while($i <= 10);
echo "<br>"." Sum of the total even numbers between 1-10 ::"." $k";
echo "<br>";
$j = 1;
$l = 0;
echo "List of the ODD Numbers between 1-10:: ";
do{
if($j%2!=0){
echo " $j " ." , ";
$l=$l+$j;
}
$j=$j+1;
}while($j <= 10);
echo "<br>"." Sum of the total odd numbers between 1-10 ::"." $l";
echo "<br>";
echo "<br>"." Sum of the total natural numbers between 1-10 ::"." $m";
echo "<br>";
?>
Output:
3. For Loop
For Loop is somewhat different when we compare with the While Loop and the Do While Loop. It executes the code again and again if the certain true condition is met. Loop will run the code for certain for the number of times as we needed and it is controlled with the condition.
For Loop will have 3 parameters. They are initialization, condition, and the incrementation value inside of the For Loop parenthesis.
Syntax:
for(initialization value; condition value; incrementing value){
//Programming code statements which is need to be executed when condition of the loop becomes TRUE
}
Parameters of the For Loop:
- Initialization: In For Loop, this is the value/variable value to start the program.
- Condition: In For Loop, this is the value/variable value which is needed to be checked. If the condition becomes true then the program statements will run continuously by checking the condition.
- Incrementing/Incrementation: In For Loop, the initial value or running value in the program statements will be incremented by 1 or other as we needed based on our requirement.
Example:
The below for loop example will print the list of the natural numbers which are between 1-30 and also the sum of all the values which are between 1-30.
$i is assigned as 1 at first as an initialization value, condition as $i less than or equal to 30 with the incrementation as $ii=$i+1. For Loop will print $i value until the i value becomes 30 and $j variable’s value will store all the numbers/values of $i variable and then it will sum them one by one in the loop until the I value reaches to 30. Then the printing of the sum of all the natural numbers between 1-30 will be printed after printing all the natural numbers using the For Loop.
Code:
<?php
echo "List of the Natural Numbers between 1-30 :: ";
$j=0;
for($i=1; $i<=30; $i++){
echo "$i" . " , ";
$j=$j+$i;
}
echo "<br>";
echo "Sum of all the natural numbers between 1-30 :: ";
echo "$j";
echo "<br>";
?>
Output:
4. Foreach Loop
Foreach is one of the loop concepts of PHP which is used to iterate over the array/arrays.
Syntax:
foreach($array as $value){
//Programming code which is need to be executed
}
Example:
The below example will print the values from the $colors1 variable. $colors1 variable values are the list of the colors. Using the foreach loop concept will print the colors which are present in the array one by one.
Code:
<?php
$colors1 = array("Yellow", "Red", "Blue", "Green",);
// Colors array accessing in the loop
foreach($colors1 as $value1){
echo $value1 . "<br>";
}
?>
Output:
Recommended Articles
This is a guide to PHP Loops. Here we discuss the introduction and different loops of PHP with syntax and parameters along with examples and code implementation. You may also have a look at the following articles to learn more –
5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses