Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I agree that the issue mentioned above isn't that bad - as a long time PHP developer (and PHP admirer) one gotcha that still really annoys me is persistence of key refs outside of foreach loops and I present this eval[1] as a wonderful example of why you should really prefer array_map for mutating array values or be very pedantic about unsetting variables when you leave a foreach using ref loop.

1. https://3v4l.org/99pMn



Except that if you do anything other than mapping, you start taking a performance hit. If you want to use array_map with array_filter, you're now iterating your array twice. Also, if you ever use array_filter, you almost certainly need to wrap it in array_values, so now you're looping N times.

It's no wonder people still lean on the broken foreach. I always use unset() after foreach. It makes me die inside a little.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: