i

PHP Tutorial

PHP Loops

Loops are used when you want to execute code multiple times until a condition is met. So, instead of writing the block of code, again and again, you can write a loop that executes the same block until a specific condition is met. Like other programming languages, we have different types of loops in PHP.

  • While loop

  • Do While loop

  • For loop 

  • Foreach loop

Let us discuss the different types of loops in php.

>While loop

While loop checks the condition at the entry and loops until the condition specified is true. It is also known as an entry-level loop as it checks the condition in the start and enters if the condition is true and executes the block of code inside the loop until the condition remains true.

Syntax of while loop is as below

while (condition) { code that needs to be executed}

For example, if you want to print numbers from 1 to 10, you can write the below while loop.

$num=1;

while($num<=10)

{

echo “The number is : $num
”;

$num++;

}

?>

In the above example, firstly we have initialized the number as 1 then it enters the loop and checks if 1 is less than 10 and executes the statement. Then we increment the number and again repeat the loop till number is less than or equal to 10.

>Do while loop

Do while loop also executes the condition till it is true, but the difference is it firsts executes the statement and then checks the condition to decide whether to run the loop again or not. It is also known as exit control loop as the code executes at least one time and then the only condition is checked to exit or again run the code.

Syntax of the do-while loop is as below

do{ code that needs to be executed}

while(condition);

For example, if we want to print numbers from 1 to 10, you can write the below do-while loop.

$num=1;

do

{

echo “The number is : $num
”;

$num++;

} while($num<=10)

?>

In the above example, firstly we have initialized the number as one then it enters the loop and executes the statement and increment the number. Then it checks the condition and again repeats the loop till number is less than or equal to 10.

The difference is that the code runs at least one time in the do-while loop. If we change the value of $num=12 in the above example, the output will be The number is:12 as it will execute the statement and then check the condition.

>For loop 

When the user is aware of how many times the code should be executed, then for loop is used. So, if you know the number of iterations for which the code needs to be executed, you can write a for loop. These loops are also called entry-controlled loops as the condition is checked before the statements are executed. A variable is used to control the for loop and is executed till the test condition is true.

The syntax for For loop is as below.

for(initialize counter; test condition ; update counter)

{

code that needs to be executed

}

Initialize counter sets a value for the counter. Test condition checks the condition for every iteration and when it is false, the loop ends. Update counter increases or decreases the counter value.

For example, If you want to print odd numbers from 1 to 10, you can write the below code.

for($number = 0; $number <=10; $number +=2)

{

           echo “ Odd number is $number \n”;

}

?>

>Foreach loop

When you use arrays and want to repeat some set of statements for each element in an array for multiple times, then foreach loop is used. It only works on arrays, and an array element is assigned for every counter of the loop, and it goes on increasing for the next array element.

The syntax for foreach loop is as below

foreach ( array_name as value)

{ code that needs to be executed}

For example, if you have an array that lists cars and you want to print all the car names, you can write the below loop.

$cars = array (“Audi”, “BMW”, “Swift”);

foreach ($cars as $value)

{

           echo “Car available is $value
”;

}?>