PHP foreach Loop | 2 Ways to Use it

PHP foreach loop featured image

What is PHP foreach loop?

The foreach loop is used to iterate through array elements in PHP.

For example,

$arr_numbers = array(5,10,15,20,25);

foreach($arr_numbers as $i){
    echo $i . "<br />";
}

Syntax of foreach loop

This is how you can use the foreach loop:

foreach($array_name as $value){

//Statement to execute

}

And you can use foreach in this way as well:

foreach($array_name as $key =>$value){

//Statement to execute

Both of these methods of using foreach are explained in the last part of this tutorial, after the examples below.

Example of using foreach loop with numeric array

  • In this example, we have created an array of five elements with numeric values.
  • After that, a foreach PHP loop is used to iterate through that array.
  • Inside the foreach loop, we used a PHP echo statement to display the array values.

See the example code and output:

$arr_numbers = array(5,10,15,20,25);

foreach($arr_numbers as $i){
    echo $i . "<br />";
}

Output:

5
10
15
20
25

An example with array key and values

In this example, we will use the other way of using the foreach loop i.e. including the key as well.

For that, we have created an associative array of three elements.

The $salaries array is defined followed by three employee names (acting as keys) and salaries as values.

$salaries = array(); 
$salaries["Anna"] = 5000;
$salaries["John"] = 6000;
$salaries["Aamir"] = 10000;

foreach($salaries as $key_arr => $val_arr){
    echo $key_arr . " = " .$val_arr ."<br />";
}

Output:

Anna = 5000
John = 6000
Aamir = 10000

An example to change array element values in foreach loop

You can modify the values of the elements of the given array by using the foreach loop. For that, use the “&” before the “$” for the value variable e.g.
&$value_of_element

The value will be modified by reference. To make it clearer, see the following example.

  • In this example, we have created a numeric array of five elements.
  • Then we simply used a foreach loop to display array element values.
  • After that, we used another foreach loop where the $value_of_element is preceded by “&”.
  • Inside curly braces, we assigned new values to the array elements.
  • To see the difference between array before and after assigning the new values, the array is displayed by using the print_r function, which is used to display the visual representation of the given array.

See the demo and code by clicking the link or image below:

$arr_num = array(5, 10, 15, 20, 25);
foreach ($arr_num as $value) {
   echo $value . "<BR/>";
}
foreach ($arr_num as &$value) {
    $value = $value * 2;
}

print_r ($arr_num);

Output:

5
10
15
20
25
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )

Why do we use PHP foreach?

The PHP foreach loop is used to manipulate arrays.

The foreach will loop through each element of the given array.

You can also use a simple for loop to manipulate arrays e.g. by using the length property to get the length of the array and then using it as the max operator. However, foreach makes it quite simpler which is specifically provided to use with arrays.

If you work with MySQL or some other database, then its purpose will be even clearer. For example, fetching multiple rows from a table of MySQL database and assigning it to an array. After that, looping through the array elements along with performing some action by using the foreach loop.

Also, note that you can use the foreach loop with an array or objects only.

How to use foreach loop

As described in the first section, there are two ways to use the foreach loop in PHP. Both are described below.

The syntax to use first method is:

foreach($array_name as $value){

echo $value

}
  • In this way of using the foreach loop, you have to specify the array name followed by a $value variable.
  • For each iteration, the current array element’s value is assigned to the $value variable.
  • After completing the iteration, it will be moved to the next element until all array elements are entertained.

The syntax to use second way

foreach ($array_name as $key_of_element => $value_of_element){

//code to be executed here

}

As apparent from the syntax, this is quite useful for PHP associative arrays, where we use key/value pairs.

So, for each iteration, the value of the current element will be assigned to $value_of_element variable, just like in the first method.

Plus, the key of the element is assigned to the $key_of_element variable, which is additional in this method.

So, if you are working with numeric arrays you can use the first method, where you do not need keys of elements.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!