
The foreach Loop in PHP
In the previous lessons, we have learned about three loops (for loop, while loop, and do-while loop). Today, we will learn about another loop called the foreach loop. The foreach loop in PHP is used to iterate over elements in an array. It is widely used in PHP projects because of its simplicity, and almost everyone likes it for that reason.
1. The syntax of the foreach loop in PHP is as follows:
The syntax of the foreach loop in PHP is as follows:
<?php foreach ($array as $key => $value){ // Code statements } ?>
Or:
<?php foreach ($array as $value){ // Code statements } ?>
In the above statement, $array
is the array to be iterated, $key
represents the index number (for indexed arrays) or the key (for associative arrays), and $value
represents the value of the element at the $key
position.
Example 1:
<?php // List of years $years = array( 1990, 1991, 1992, 1993, 1994, 1995 ); // Using foreach to output the years in $years foreach ($years as $key => $value){ echo $value; } ?>
The foreach loop automatically iterates through each element in the array until it reaches the last element. In the example above, $years
is the array we pass in, and $key
and $value
are the two parameters that are automatically passed with their respective values in each iteration, which we can use accordingly. The output displayed on the screen is:
0 => 1990 1 => 1991 2 => 1992 3 => 1993 4 => 1994 5 => 1995
If you pay close attention, you will notice that in the loop, I only pass $years
, while $key
and $value
remain unchanged. Does it always have to be that way? The answer is no, you can actually assign them any variable names you prefer. For example, the following program is equivalent:
<?php // List of years $years = array( 1990, 1991, 1992, 1993, 1994, 1995 ); // Use foreach to output the years in $years foreach ($years as $index => $value){ echo $index . ' => ' . $value; } ?>
With the given problem, we can use the second syntax of the foreach loop in PHP to solve it:
<?php // List of years $years = array( 1990, 1991, 1992, 1993, 1994, 1995 ); // Use foreach to output the years in $years foreach ($years as $value){ echo $value; } ?>
In this example, we only retrieve the $value
because we didn’t pass the $key
variable. The output on the screen is:
1990 1991 1992 1993 1994 1995
Example 2:
In this example, the list of students passed is an associative array in the format of student_id => student_name. Inside the loop, we output the corresponding student ID and name. With this approach, we can only retrieve the name but not the student ID.
<?php // List of student IDs and corresponding names $students = array( 'ST001' => 'Elizabeth', 'ST002' => 'Christopher', 'ST003' => 'Jennifer', 'ST004' => 'Matthew', 'ST005' => 'Rebecca' ); // Output the list of students foreach ($students as $studentName){ echo $studentName . '<br/>'; } ?>
That’s the difference between the two looping methods.
To have a better understanding of the foreach loop in PHP, I invite you to read the article “The essence of the foreach loop in PHP”. It discusses the speed and operation process of the foreach loop.
2. Conclusion:
At the end of this tutorial, I hope you have grasped how to use the foreach loop in PHP to handle arrays. Please note that the foreach loop can be nested to handle multi-dimensional arrays. I won’t provide an example here as it would make the tutorial too long and potentially boring. Additionally, we will cover this topic in a future section. In the next tutorial, we will learn about the break, continue, go to, die, and exit statements, which are used to terminate loops or exit the program.
Related Articales
Comments
-
Download and Install Vertrigo Server
May 25, 2023.83 views -
Declaring variables in PHP, common types of variables encountered
May 25, 2023.83 views -
Data types in PHP and their corresponding variable types
May 25, 2023.83 views -
Operators and expressions in PHP
May 25, 2023.83 views -
The if else statement in PHP
May 25, 2023.83 views -
The switch case statement in PHP
May 25, 2023.83 views -
The for loop in PHP
May 25, 2023.83 views -
While and do-while loops in PHP
May 25, 2023.83 views -
The foreach Loop in PHP
May 25, 2023.83 views -
The break, continue, goto, die, exit statements in PHP
May 25, 2023.83 views -
Building Functions in PHP
May 25, 2023.83 views -
Recursive Algorithm in PHP
May 25, 2023.83 views -
Bubble Sort Algorithm in PHP
May 25, 2023.83 views -
Linear Search Algorithm in PHP
May 25, 2023.83 views -
The technique of sentry placement in PHP
May 25, 2023.83 views -
Flagging technique in PHP
May 25, 2023.83 views -
Selection Sort Algorithm in PHP
May 25, 2023.83 views
Tags
© copyright 2021 Courseplus