In this lesson, you‘ll learn to use the foreach loop, which is designed to handle arrays and certain types of PHP objects. The foreach loop can be used in two different ways. The first gives you the access to the value of each array element. The syntax looks like this. The first point to note is that foreach is written as one word. It‘s a key word and if you put a space between for and each it won‘t work. Inside the parenthesis, you have the name of the array you want to loop through followed by the AS keyword. And then, a temporary variable to store the value of the current array element each time the loop runs.
So, let‘s see how that works. I‘m going to use a foreach loop to loop through this characters array to build an unordered list. So, I need to add a PHP code block between the UL text. Then for each, and the name of the array, which is characters, the AS keyword and then a variable to store the value of each array element.
It doesn‘t matter what you call the variable. I‘m going to use name, and then some curly braces for the code that will be executed in the loop, and so we just echo. I‘ll use a double quoted string, LI tag, name, closing LI tag. And that‘s it.
It‘s quite simple and quite elegant to use a foreach loop with an array like this. Let‘s just save that page and view it in a browser. And there we have a nice unordered list. As the name suggests, a foreach loop accesses each array element and unlike a for loop, it doesn‘t use a counter. So, if you don‘t want to loop through the whole array, it‘s better to use a for loop and break out of the loop when the counter reaches a certain value.
Alternatively, you need to initialize a counter outside of foreach loop and then increment it each time the loop runs. But this is best used if you want to use the whole of the array. The other way of using a foreach loop, gives you access to both the key and value of each array element. Using the foreach loop like this is particularly useful for associative arrays and the syntax is very similar to an associative of array. After the AS keyword, you add two temporary variables separated by the same operator that‘s used to create name value pairs in the associative array.
The first variable stores the key of the current element and the second one stores its value. So, let‘s see that in operation. Here I‘ve got an associative array and what I‘m going to do is I‘m going to put it in the body of this HTML file as a series of paragraphs. So, foreach, of course, we need to have a PHP block first. So, inside the PHP block, foreach.
Then the name of the array is descriptions, the AS keyword. And then, I need two variables. I‘m going to use key which is very simple and then the operator, which is an equal sign, followed by a greater than sign. And I‘ll use description rather than descriptions because that‘s what it is that I‘m showing. And then, a pair of curly braces.
And inside there, we‘re going to Echo and we‘ll use double quoted string and a P Tag and we‘ll start off with Key Is, and then description followed by a period. And then closed p. There we are.
Let‘s just save that, and view it in a browser. So, what‘s happened here is that the key has been displayed. In this case, it‘s Earth, followed by the word Is, and then by the description. So, we‘ve got the key is description. And the same has happened with each array element in the associate of array. So, the foreach loop offers a convenient way of looping through every element of an array. In it‘s basic form, it stores the value of the current array element in a temporary variable. And when used with an associative array, you can define two temporary variables. One for the current elements key, and the other for its value.