-1

I'm upgrading old in-house PHP code for my company and I stumbled upon this piece of code:

foreach ($array as $key => &$value) {
    // do something
}

what's the meaning of &$value and can I safely assume this is a mistype?

Puka
  • 1,485
  • 1
  • 14
  • 33
  • 2
    Hint: From [documentation](https://www.php.net/manual/en/control-structures.foreach.php): _In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference_. – Zhorov Apr 09 '21 at 12:26

1 Answers1

2

It is not mistype. It is reference and it means to access the same variable content by different names. If you modify the value of $value, then you would also modify the original value inside $array.

You can read more about it at References Explained, Php.net

Jax-p
  • 7,225
  • 4
  • 28
  • 58