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?
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?
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