How to use foreach in php – A Complete Guide

Foreach is a very useful construct in PHP that allows you to loop through arrays and other iterable data structures. Here is a complete guide on how to use foreach in PHP:

Basic syntax

The basic syntax of foreach in PHP is as follows:

php
foreach ($array as $value) { // code to execute }

In the above code, $array is the array you want to loop through, and $value is a variable that represents the current value in the loop. You can then perform some action on $value within the loop.

Looping through arrays

One of the most common use cases for foreach in PHP is to loop through arrays. Here is an example of how to do that:

php
$fruits = array(“apple”, “banana”, “orange”); foreach ($fruits as $fruit) { echo $fruit . “<br>”; }

In the above code, we have an array called $fruits, and we use foreach to loop through each item in the array and print it out using the echo statement.

Looping through associative arrays

You can also use foreach to loop through associative arrays, which are arrays with named keys. Here is an example:

php
$ages = array(“Peter”=>32, “John”=>28, “Mary”=>20); foreach ($ages as $key => $value) { echo “$key is $value years old.<br>”; }

In the above code, we have an associative array called $ages, and we use foreach to loop through each item in the array and print out the key and value using the echo statement.

Using continue and break

You can use continue and break statements within a foreach loop to control the flow of the loop. Here is an example:

php
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); foreach ($numbers as $number) { if ($number == 5) { continue; // skip number 5 } if ($number == 8) { break; // stop the loop when we get to number 8 } echo $number . “<br>”; }

In the above code, we have an array called $numbers, and we use foreach to loop through each item in the array. We use continue to skip the number 5, and break to stop the loop when we get to number 8.

Using list()

You can also use the list() function within a foreach loop to assign multiple variables at once. Here is an example:

php
$person = array(“John”, “Doe”, 32); foreach ($person as $value) { echo $value . “<br>”; } list($firstName, $lastName, $age) = $person; echo “First name: $firstName<br>”; echo “Last name: $lastName<br>”; echo “Age: $age<br>”;

In the above code, we have an array called $person, and we use foreach to loop through each item in the array and print it out. We then use the list() function to assign the values in the array to three separate variables, which we can then use to print out each person’s first name, last name, and age.

Using foreach with objects

You can also use foreach to loop through properties in an object. Here is an example:

php
class Person {
public $firstName; public $lastName; public $age; public function __construct($firstName, $lastName, $age) { $this->firstName = $firstName; $this->lastName = $lastName; $this->age = $age; }
}

$person = new Person(“John”, “Doe”, 32);

foreach ($person as $property => $value) { echo “$property: $value<br>”; }

####In the above code, we have a `Person` class with three properties: `firstName`, `lastName`, and `age`. We then create a new instance of the `Person` class called `$person`, and use `foreach` to loop through each property in the object and print it out.

### Using `foreach` with references You can use references with `foreach` to modify the original array or object within the loop.
Here is an example:

php
“`php $numbers = array(1, 2, 3, 4, 5);
foreach ($numbers as &$number) {
$number = $number * 2;
}
print_r($numbers);

In the above code, we have an array called $numbers, and we use foreach with a reference to each value in the array. We then multiply each value by 2 within the loop, which modifies the original array. Finally, we print out the modified array using the print_r() function.

Conclusion
In conclusion, foreach is a very useful construct in PHP that allows you to loop through arrays, associative arrays, objects, and other iterable data structures. By using foreach, you can perform some action on each item in the loop, and even modify the original array or object within the loop.