What Is the PHP Array Foreach Loop and How It Is Different From for Loop

In PHP language, there are different types of arrays like an indexed array, associative array, and multidimensional array. We use them according to our requirements. Sometimes we need to print the values of an array. We can print them using the echo method as follows:

Example 1

Suppose we have an array of different shapes, and we want to print the values of an array.

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle","rectangle","square","triangle");

echo $shapes[0]."<br>";
echo $shapes[1]."<br>";
echo $shapes[2]."<br>";
echo $shapes[3]."<br>";

?>

</body>
</html>

Output:

circle
rectangle
square
triangle

Explanation:

In the above example, we print the values of the array using array index no.

We should use for loops of PHP to make the program small and systematic. We can write the above example using for a loop as follows:

Example 2

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle","rectangle","square","triangle");

for($i=0;$i<5;$i++){
echo $shapes[$i]."<br>";
}

?>

</body>
</html>

Output:

circle
rectangle
square
triangle

Explanation:

Here we use for loop with counter variable a which circulates the loop five times to print the values of an array.

We can do the same thing using the foreach method of PHP.

What is the foreach method, and how it is different from for loop?

The foreach is one of the methods used for printing the values of an array.

Syntax:

<?php
foreach ($arr as $value){
//code;
}
?>

Explanation:

Here $arr is the name of an array on which operations are going to be performed.

$value is a temporary name assigned to the values of an array for accessing them inside a foreach loop.

We can also write the syntax as below:

<?php
foreach ($arr as $value) :
//code;
endfroeach;
?>

The above syntax also generates the same result.

Also Read: PHP Array Functions

Example 3

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle","rectangle","square","triangle");

foreach($shapes as $value){
echo $value."<br>";
}

?>

</body>
</html>

Output:

circle
rectangle
square
triangle

Explanation:

In the above code, we get the output as same as example 2.

Note: Sometimes, when the user creates an array by assigning the keys to its value, then we can not use for loop for printing the array.

Suppose we have an array:

$arr = array(“circle”=>”blue”,”rectangle”=>”red”,”square”=>”black”,”triangle”=>”pink”);

And we want to print the array. At this time, we cannot use for loop with its counter variable.

We must use a foreach method for printing the array. We use the following syntax for echoing the array having the keys and values.

Syntax:

<?php
foreach ($array as $key => $value){
//code;
}
?>

Explanation:

  • Here array attribute is the name of an array on which operations are going to be performed.
  • $key is a temporary name assigned to the keys of an array for accessing them inside a foreach loop.
  • $value is a temporary name assigned to the values of an array for accessing them inside a foreach loop.

Example 4

Suppose we have an array:

$arr = array(“circle”=>”blue”,”rectangle”=>”red”,”square”=>”black”,”triangle”=>”pink”);

We want to print the key with its value.

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle"=>"blue","rectangle"=>"red","square"=>"red","triangle"=>"pink");

foreach($shapes as $key => $value){
echo "Key = ".$key." and value = ".$value."<br>";
}

?>

</body>
</html>

Output:

Key = circle and value = blue
Key = rectangle and value = red
Key = square and value = red
Key = triangle and value = pink

Explanation:

Here we assign the keys of the array in $key and values of the array in $value variable and access them inside the foreach loop.

We can also write the syntax as below:

<?php
foreach ($array as $key => $value) :
//code;
endfroeach;
?>

The above syntax also generates the same result.

Example 5

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle"=>"blue","rectangle"=>"red","square"=>"red","triangle"=>"pink");

foreach($shapes as $key => $value):
echo "Key = ".$key." and value = ".$value."<br>";
endforeach;

?>

</body>
</html>

Output:

Key = circle and value = blue
Key = rectangle and value = red
Key = square and value = red
Key = triangle and value = pink

Explanation:

Here we use a new syntax of the foreach loop, which also generates the same output.

Conclusion

This is all about the foreach method of PHP. In indexed array, we can use the for loop or foreach loop for printing the values of an array.

But in the associative array is necessary to use a foreach loop for accessing the values of an array.

I hope you found this post fully informative and helpful.

Thank you for reading 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *