Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
C0D4681465yIt's kinda a bug, but it's also a reference in memory issue too, which is why I very rarely use variables by reference in php unless it's to conserve memory limits which I can't get around.
Basically, and if I go look at that link it's probably safe to say it says the same thing, you no longet have a localised variable for the scope of the foreach() as you pushed it as a reference giving the second $item an inherited value, you should unset() the first $item
Also, can you do my eyes a favour and hit the Enter key just before that ")" in your first array? -
@inaba IIRC they are only value types and "references" which are actually aliases
-
Lythenas2455yMy problem here is that $item is not scoped to the foreach. That really drives me nuts. That would make this behavior impossible and is in general what you actually expect.
-
ZioCain27115y
-
inaba46255y@12bitfloat https://php.net/manual/en/...
> One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default".
Well that is how it's supposed to be I gue-
> This is not completely true. This
Oh- oh god -
@inaba I've generally gotten the impression that mistaking PHP for a sane language can lead to much mental anguish, and I've never used PHP. It's really it's own thing, design-wise.
-
nitnip18085yIt is not a bug. Read the accepted answer for the technical explanation.
If you want to avoid this, just use a different name for your references.
foreach($arr as &$ref) { ... } print_r($arr);
foreach($arr as $val) { ... } print_r($arr);
Related Rants
I swear I NEVER hated PHP before, but after seeing this shit, I just can't love it anymore, I feel betrayed
I mean, WTF? Just bc I'm using the variable as a reference in the NEXT loop the LAST element changes? What even is the fucking reason?
Yeah, I read the answer on StackOverflow, but seriously...
SO ref: https://stackoverflow.com/questions...
rant
not really a bug
reference
loop
stackoverflow
bug
php