Loops in PHP

PHP Loops:

Loops in PHP provide a way to repeat commands and control how many times they are repeated.

Looping statements in PHP are used to execute the same block of code a specified number of times.

Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.

Loops in PHP

Why do we want to use loops in PHP code?

  • Do something for a given number of times or for every object in a collection of objects.
  • For every radio button in a form, see if it is checked.
  • For every month of the year, charge $100 against the balance.
  • Calculate the sum of all the numbers in a list.

In PHP we have the following looping statements:

  • While Loop: loops through a block of code if and as long as a specified condition is true.
  • Do While Loop: loops through a block of code once, and then repeats the loop as long as a special condition is true.
  • For loop: loops through a block of code a specified number of times.
  • Foreach Loop: loops through a block of code for each element in an array.

While Loop in PHP:

The while loop will execute a block of code if and as long as a condition is true.

Syntax:

while (condition)
code to be executed;

Example of while loop:

<?php
   $a=1;
   while($a<=5) {
    echo "The number is " . $a . "<br/>";
    $a++;
   }
?>

Do while Loop in PHP:

The do…while loop in php will execute a block of code at least once-it then will repeat the loop as long as a condition is true.

Syntax:

do
  {
  code to be executed;
  }
while (condition);

Example of do-while loop:

<?php
    $a=0;
    do
 	{
   	$a++;
   	echo "The number is " . $a . "<br/>";
 	}
    while ($a<5);
?>

For Loop in PHP:

The for loops in PHP is used when you know how many times you want to execute a statement or a list of statements.

Syntax:

<?php
   for (initialization; condition; increment) {
 	code to be executed;
}
?> 

The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop.

If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

Example of for loop:

<?php
  for ($i=1; $i<=5; $i++) {
      echo $i."<br/>";
  }
?>

Foreach Loop in PHP:

The for-each loop is used to loop through arrays. For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) so on the next loop, you’ll be looking at the next element.

Syntax:

<?php
  foreach(array as value) {
 	code to be executed;
}
?>

Example of foreach loop:

<?php
$array=array(“red", “green", “black");
  foreach($arras $value) {
 	echo "Value: " . $value . "<br/>";
  }
?>

For more information visit php.net

LIKE THIS ARTICLE?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn