Web Statistics

for & foreach

Forever is composed of nows. - Emily Dickinson

for

The for loop is a valuable tool to have in the bag of basic skills for any programmer, and in PHP it is the most complex loop. The syntax of the for loop varies from one code language to another. In Java and Python it resembles that of the foreach loop in PHP; however the for loop in PHP is equal to that of the for loop in C#. Lets look at the syntax in the example below and then break it down.

				
for ($x=1; $x<=10; $x++)
{
	print "$x<br />";
}
				
			

The for loop in PHP requires three expressions. The first expression ($x=1) is evaluated and executed once. In this example the variable $i is set to the integer 1. The second expression ($x<=10) is evaluated at the beginning of each iteration. As long as the result of the expression returns true the iteration continues. A false result in the iteration breaks the iteration and the loop exits. The third expression ($x++) is evaluated and executed at the end of each iteration.

Lets dive inside...
Once the process encounters the for loop the first expression is triggered. This sets the variable $x to 1. Then the second expression is executed. Here the script checks if the value of $x is less than 10, and in this iteration the result is true. Now the process enters the code block inside the loop, and the script prints out "$x<br />". The code block is complete so the third expression is executed incrementing the value of $x to 2. Thus ending the first iteration and beginning the second iteration. These iterations will continue till the second expression returns false.

foreach

While the for loop can iterate through some arrays it's complexity is impractical for the vast majority of situation when needing to iterate through arrays. This is where the foreach loop can be of value.

The foreach loop takes one array expression and assigns each element to a variable as it cycles through the iterations.

On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element). - PHP.net

				
$colors = array('red','green','blue');
foreach ($colors as $color)
{
	print "$color<br />";
}
				
			

In the example above the foreach loop iterates through the index array $colors. The result of the process is as follows:

				
red<br />
green<br />
blue<br />
				
			

When encountering associative arrays it is sometimes best to know what the key of the array element is in the iteration. This can be handeled as such:

				
$colors = array(
    'key1'=>'red',
	'key2'=>'green',
	'key3'=>'blue'
	);
foreach ($colors as $key => $color)
{
	print "$key = $color<br />";
}
				
			

Which results in:

				
key1 = red<br />
key2 = green<br />
key3 = blue<br />
				
			

One last note to make about the foreach loop

In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.
In PHP 7, foreach does not use the internal array pointer. - PHP.net

Up Next

Break, Continue, & Goto