Get DevWP - WordPress Development Theme

PHP Loops

Besides conditional statements like we’ve covered in previous tutorials, PHP also supports various types of looping mechanisms.

There are four basic types of loops and in this article I will cover all four.

Here are the four types of loops.

  1. While Loop
  2. For Loop
  3. Foreach Loop
  4. do while Loop

What are PHP Loops Used for?

Loops are used to execute the same code, over and over again as long as the condition is met. This is ideal since it eliminates the need to write the same code over and over again which can be tedious and lead to errors.

Looping is also ideal when used for a blogging/cms platform like WordPress and others. The articles are saved in the database and your while loop is displaying each article or excerpt as long as articles are available to view.

You can also use loops to go through a list of names or emails or numbers or whatever can be looped.

Loops are very powerful and when correctly implemented, can make your life as a coder, much easier.

Make sure to copy the code and use them locally so you can alter the code and make it do something different.

The Best Way to Learn How to Code, is to Code.


PHP While Loop

While loops are probably the easiest loops you will use. You will find many uses for it. While loops are best used when you don’t know how many times you’ll need to loop over something.

Below is an example of a while loop in action.


<?php
$i = 1;

while ( $i <= 10 ) {
	echo "The number is: $i <br>";
	$i ++;
}
?>

Let’s Review the Code

  • first you will notice we have a variable with it’s assigned value $i = 1;
  • then we open up with the while ( $i <= 10 ) { which is testing the condition in between the parenthesis. Since we set $i to the value of 1, as long as $i is less than or equal to 10, we will output the next part of the code
  • we echo "The number is: $i <br>"; continuously until the condition being tested against is no longer met.
  • we also need to make sure we increment $i so that we don’t end up in an infinite loop situation. That’s where the next line of code comes in $i ++;
  • We then close off with the }

The example above is a basic implementation of the while loop.

PHP for Loop

The for loop is best used when you know the amount of times you want to loop over something. It’s implemented differently than the while loop. Take a look at the example below.


<?php
for ( $i = 1; $i <= 10; $i ++ ) {
	echo "The number is: $i <br>";
}
?>

Let’s Review the Code

Unlike a while loop where we first need to create a variable outside of the looping mechanism, with a for loop, the variable is set inside the parenthesis.

  • we start off with for
  • right after the opening parenthesis we set our variable $i = 1;
  • then we test $i <= 10;
  • followed by incrementing the value of $i $i ++
  • we then have the closing part of the parenthesis followed by the opening curly brace ) {
  • the next line of code handles outputting the current value echo "The number is: $i <br>";
  • finally we have our closing curly brace }

The key thing to remember is to make sure you initialize the variable first and use a semi-colon then do the test to check if it’s less than or equal to 10 followed by another semi-colon and finally increment the variable $i.


PHP foreach loops

Foreach loops are used with arrays which we’ll cover in depth in another tutorial. This loop is specially made only for arrays. You could use a for loop with arrays and in some cases it may make sense to do so, but primarily you’ll be using the foreach loop.

Let’s look at an example of a foreach loop in action.


<?php
$colors = array( "blue", "purple", "green", "red", "yellow", "black" );

foreach ( $colors as $value ) {
	echo "$value <br>";
}
?>

Let’s Review the Code

  • first thing we do is create a variable called $colors which is assigned the value of an array( "blue", "purple", "green", "red", "yellow", "black" ); which as you can see has several colors each in the format of a string enclosed in quotes and separated by a comma
  • you will notice the colors are inside the parenthesis and that line of code ends with a semi-colon
  • we then move onto the next line foreach ( $colors as $value ) {
  • after the foreach and in between the parenthesis, you will notice we take the variable $colors and say as $value which means each actual color will be in the temporary & reusable variable $value
  • on the next line we echo "$value <br>"; which is going to output each color
  • finally we close off the curly brace }

PHP do while Loops

I’ve purposely saved this one for last even though it’s often grouped with the While Loop. The reason why I’ve done this is because the Do While loop isn’t used as often as the others.

Even though it isn’t often used, you might find it useful for a particular use case.

The Do While loop executes a block of code first and then continues to loop over the block of code as long as the condition being checked for is still true. It sounds similar to the While loop but below you will see the difference.


<?php
$i = 0;
do {
  echo $i;
} while ( $i > 0 );
?>

Do you see what happened above?

Let’s Review the Code

  • The first thing to notice is that we initialized the variable $i = 0;
  • we then open up with do {
  • then we echo $i;
  • finally we close off with } while ( $i > 0 );

The Output from above is 0

Even though $i isn’t greater than zero, we still process the first part of the do while loop and then we test the condition to see if anything else needs to be processed.



View Our Themes

Let's Connect