Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

The foreach looping construct

foreach is a construct that is only supported in PHP version 4 and above. If you are still using PHP version 3, this will not work for you. You will need to download Open's new Window a later release of PHP.

The foreach construct is used mainly for traversing an array. If you remember, our earlier examples of traversing arrays were just examples with little explanation. This is because they involved looping which we had not covered yet. Now that you have learnt control structures you should consider going back to these examples and try to get the logic out of them...

Back to the foreach:

A foreach ("And hey! Don't put a space between the for and each. Ok?") can be applied in one of two ways:

1. foreach($array as $value){
	PHP statement 1;
	PHP statement 2;
	...
    }

2. foreach($array as $key => $value){
	PHP statement 1;
	PHP statement 2;
	...
    }

The first construct loops through the array (our array is $array in this case) and assigns the current value of the array - $array(current) to $value. Notice you DON'T need to use the current keyword. On each iteration, the statement block is also evaluated.

To traverse our $fruits array (created in the previous exercise) using this example, we will code as follows:

foreach($fruits as $item){
	echo $item . "<br>";
} 

This code snippet will loop the array. Every time it does, the current value ('room value') is assigned to the variable $item. Our statement block is made of one line that prints the cell content and a <br> using the echo() command.

As indicated earlier, the second variation of the foreach construct uses the following syntax:

 foreach($array as $key => $value){
	PHP statement 1;
	PHP statement 2;
	...
 }

This second variant of the foreach loop construct is more or less like the former - only that it extracts two things: the key and the value. If you remember, we said that the 'room number' (or 'room name' for Associative Arrays) is called a key, and that the 'room contents' are called the value.

Using this second form of the foreach construct, we can grab both the key (room name/number), and the value (room value/occupants)... {I hope you are not bored with this room stuff... yet}

As an example, let's consider the following Associative Array:

$employee = Array("Name" => "John Bremmer",
		  "DOB" => "4/15/2000",
		  "Salary" => "52000",
		  "Marital Status" => "Single",
		  "Dept" => "IT007");

Now, supposing we wanted to print the employee details, we could use the following code snippet:

echo "<table border cellspacing='0'>";
foreach($employee as $field => $data){
	echo "<tr><td bgcolor='gray'>" . $field .
	"</td><td>" . $data . "</td></tr>";
}
echo "</table>";

This code snippet will produce something like this:

NameJohn Bremmer
DOB4/15/200
Salary52000
Marital StatusSingle
DeptIT007

Well?

I sure hope things are OK and that we are sailing in the same boat, rocking at the same speed.

That's all for constructs that are otherwise referred to as control structures. This far, we have learnt how to make decisions and how to loop using the STANDARD SYNTAX. There are other WAYS of applying the sytax - THE ALTERNATIVE SYNTAX.

Let's quickly look at The Alternative Syntax

<< The for Iteration Construct | The ALTERNATIVE Syntax >>

JamHitz Productions