Oh, I'm sorry, I uploaded the wrong file to check:
<!DOCTYPE html>
<html lang="en">
<head>
<title> Foreach loop </title>
</head>
<body>
<!-- Foreach loops are a little different from the previous two types of loops. Foreach loops are going to take an array and loop through each item of the array until it gets to the end. If you think back, while loops and for loops know whether to quit or to keep looping by testing if a condition has been met. Foreach loops, on the other hand, are going to know whether to quit or whether there are items left in the array to loop over. The end of the array is what ends the looping. There's really just two approaches to looping. Let's use a real-world scenario for a second. Imagine that I have a classroom with 20 kids in it.
And I have to put name tags on all of them. Now, I could just go around the room. And I could put a name tag on each kid and keep track of the numbers as I went. And knowing that there are 20 kids, when I had gotten to the number 20, I'd know I was done. Or, since I know that I have a finite set, I could just line them all up, start at the beginning and move down the line. And when I got to the end of the line, I would know I was done. I wouldn't have to keep track of the count as I was going. I mean, I could count. I could certainly know that I was on the fourth kid, but I wouldn't have to.
I could just start at the first kid, go to the second kid, and then the third kid, until I got to the end of the line. That's how a foreach loop works. The syntax is also a little bit different. -->
<!-- FOREACH LOOP GENERAL FORMAT FOR ARRAYS:
foreach ($array as $value) {
statement;
}
-->
<!-- FOREACH LOOP GENERAL FORMAT FOR ASSOCIATIVE ARRAYS:
foreach ($array as $key => $value) {
statement
}
-->
<!-- EXAMPLE 1 -->
<p> Example 1: Regular Array </p>
<?php
$ages = array(4,8,15,16,23,42);
foreach ($ages as $age) {
echo "Age: {$age} <br />";
}
/* Explanation of above code
We have an $ages array with the numbers you see in orange contained within the array.
The foreach function will execute a command on each of the items in the array. The new variable $age that you see in the ($ages as $age) parentheses is the placeholder for the items of the array. That is to say, when the loop runs, the $age variable will sequentially become 4, then 8, then 15, then 16, and so on, so the variable $age temporarily becomes each number in the array so that the code in the foreach loop can be executed on each item of the $ages array.
The output will be a list with the string "Age: " followed by the different numbers in the array.
A FOREACH LOOP ENDS BY ITSELF, BECAUSE IT ONLY LOOPS FOR AS MANY TIMES AS THERE ARE ITEMS IN THE ARRAY!!!!!!!!!!!!!!!!!
*/
?>
<br />
<br />
<p> Example 2: Associative Array </p>
<?php
$person = array( //Don't forget the commas after each index-object pair
"first_name" => "Kevin", // Let's just call the index the "attribute" and the object the "data"
"last_name" => "Skoglund", // since that is what they represent for the contents of this example
"address" => "123 Main Street",
"city" => "Beverly Hills",
"state" => "CA",
"zip_code" => "90210"
);
foreach($person as $attribrute => $data) {
$attr_nice = ucwords(str_replace("_", " ", $attribute));
echo "{$attr_nice}: {$data} <br />";
}
/* Explanation of above code
For each of the items in the array:
the string replace (str_replace) function will take the part of the variable $attribute (the index) that has an underscore "_" , and replace it with an open space " " . So for example, we have first_name. The function will take first_name and replace the "_" with an " " in order to get "first name"
ucwords function capitalizes each word in a string. So it will take "first name" and make it "First Name"
The result of the rearrangements done by these two functions will be stored in the variable $attr_nice (attributes made to look nice for output display)
The foreach loop will then output the $attr_nice variable with the corresponding $data. */
?>
</body>
</html>
(just in case the above is unreadable, i attached the file)
foreach_loop.php