PHP foreach loop | 2 ways to use it

Syntax of PHP foreach loop

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

This is how you can use the foreach loop:

And you can use foreach in this way as well:

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 echo statement to display the array values. See the example and code by clicking the image or link below:

PHP foreach

See online demo and code

An example with array key and values

In this example, we will use the other way of using the foreach loop i.e. including 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.

PHP foreach associative array

See online demo and code

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:

PHP foreach reference

 

See online demo and code

Why 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 array or objects only.

How to use it 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:

 

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

 

As apparent from the syntax, this is quite useful for the 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.