The foreach Loop in PHP

The foreach Loop in PHP

15/05/2023
55 Lượt xem

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.

5/5 - (2 votes)
Download and Install Vertrigo Server
15/05/2023
55 Lượt xem
Operators and expressions in PHP
15/05/2023
55 Lượt xem
The if else statement in PHP
15/05/2023
55 Lượt xem
The switch case statement in PHP
15/05/2023
55 Lượt xem
The for loop in PHP
15/05/2023
55 Lượt xem
While and do-while loops in PHP
15/05/2023
55 Lượt xem
The foreach Loop in PHP
15/05/2023
55 Lượt xem
Building Functions in PHP
15/05/2023
55 Lượt xem
Recursive Algorithm in PHP
15/05/2023
55 Lượt xem
Bubble Sort Algorithm in PHP
15/05/2023
55 Lượt xem
Linear Search Algorithm in PHP
15/05/2023
55 Lượt xem
The technique of sentry placement in PHP
15/05/2023
55 Lượt xem
Flagging technique in PHP
15/05/2023
55 Lượt xem
Selection Sort Algorithm in PHP
15/05/2023
55 Lượt xem

Related Articales

Comments

© copyright 2021 Courseplus