Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

PHP - Ampersand before the variable in foreach loop

Hey everyone,

I hope you're all doing well. I have a question about using an ampersand (&) before a variable in a foreach loop in PHP.

I was going through some code and came across a foreach loop that looked like this:

$array = [1, 2, 3, 4, 5];

foreach ($array as &$value) {
// Some code here

I noticed that the variable `$value` is preceded by an ampersand (&). I haven't seen this before and I'm curious to know what it does and why it is used in this context.

Could someone please explain the significance of using the ampersand before the variable in a foreach loop? I would greatly appreciate it.

Thank you in advance for your help!

All Replies


User 2:

Hello there!

I actually stumbled upon the same curiosity a while back when working with PHP foreach loops using the ampersand (&) before the variable.

From my personal experience, using the ampersand in a foreach loop signifies that you want to access and modify the original array elements directly within the loop. This can be especially useful when you need to update or manipulate the values of the array elements.

For instance, let's say you have an array of numbers and you need to square each number in the array:

$numbers = [2, 4, 6, 8];

foreach ($numbers as &$number) {
$number = $number * $number;


The output of this code would be:

[0] => 4
[1] => 16
[2] => 36
[3] => 64

By using the ampersand (&) before `$number`, any changes made within the loop will directly modify the original elements of the `$numbers` array.

However, it's crucial to note that when using the ampersand in a foreach loop, you need to be cautious. If you don't unset the reference after the loop, it may lead to unexpected behavior in subsequent code. To avoid any issues, make sure to unset the reference once you're done with the loop:


I hope this explanation helps! If you have any more questions or need further clarification, feel free to ask.


User 1:
Hey there!

Using the ampersand (&) before the variable in a foreach loop in PHP actually creates a reference to the original array elements, rather than making a copy of them. This means that any changes made to the variable within the foreach loop will directly affect the original array.

For example, let's say we have an array of names and we want to add a prefix to each name:

$names = ['John', 'Jane', 'Michael'];

foreach ($names as &$name) {
$name = 'Mr. ' . $name;


In this case, the output would be:

[0] => Mr. John
[1] => Mr. Jane
[2] => Mr. Michael

By using the reference (&) before `$name` in the foreach loop, we directly modify the elements of the original `$names` array. If we had omitted the ampersand, any changes made to `$name` would only be local to the loop and wouldn't affect the original array.

I hope this clarifies the usage of the ampersand in foreach loops. If you have any further questions, feel free to ask!

New to LearnPHP.org Community?

Join the community