PHP - Foreach loop

  »   PHP Introduction  »   PHP Tutorial - Foreach loop

The PHP foreach loop is native function that loops a piece of code for each key/value of an array until it reaches the last array element. It only works on an array, so do not try this with expressions, or any other type of values of a variable other than arrays.

Here is the syntax of a foreach loop

phpforeach ($array as $value) {
    # run this block of code;
}

The main difference of a foreach statement vs the while or for Loops is that: while and for, will continue until some condition fails. On the other hand, foreach loop will continue until it has gone through every item of an array.

For every loop, the value of the current array key is assigned to $value and the array pointer is internally moved to the next key until the last key is reached.

Here is a simple example that illustrates how the foreach statement works:

php<?php 
$role = array("admin", "moderator", "publisher", "editor", "user"); 

foreach ($role as $value) {
    echo "Current role is: $value <br />";
}
?>

This will output:

phpCurrent role is: admin 
Current role is: moderator 
Current role is: publisher 
Current role is: editor 
Current role is: user

Get array key in foreach statement

The PHP foreach statement allows you to also recover and use the current can not only the value of it. Let's look at the current example.

php<?php 
$role = array("admin", "moderator", "publisher", "editor", "user"); 

foreach ($role as $key=>$value) {
    echo "Current role ID is $key, and his value is $value <br />";
}
?>

This will output:

phpCurrent role key is 0, and his value is admin 
Current role key is 1, and his value is moderator 
Current role key is 2, and his value is publisher 
Current role key is 3, and his value is editor 
Current role key is 4, and his value is user

This is actually very useful in everyday programming since allows you to build and loop throw multidimensional arrays and read every piece of information. But we will not get into more details for now. We'll talk more about foreach in the arrays tutorial.

How to correctly use foreach with arrays

Since you will always use foreach with array type Values, it is a good practice to check that the value of the variable is really an array. Otherwise, you will get an error.

php<?php 
$role = "anonymous"; 

foreach ($role as $value) {
    echo "Current role is: $value <br />";
}
?>

This will trigger the following error warning:

phpWarning: Invalid argument supplied for foreach() in C:\path\to\file.php on line xx

So to prevent this we can first check if the value of our $role value is actually an array we check it like this:

php<?php 
$role = "anonymous"; 

if(is_array($role)) {
	foreach ($role as $value) {
	    echo "Current role is: $value <br />";
	}
} else {
	# run a different piece of code if $role is not an array
}
?>

PHP foreach alternative syntax

The PHP foreach statements has an alternative syntax for you to use. Take a look at the next example. We are using the colon punctuation mark and the endforeach to end the syntax.

php<?php 
foreach ( $array as $value):
	# do some awesome stuff here and repeat it 10 times
endforeach;
?>