Hacker Times
new
|
past
|
comments
|
ask
|
show
|
jobs
|
submit
login
xkcdfanboy
on Feb 28, 2013
|
parent
|
context
|
favorite
| on:
How PHP's foreach works
I can't count the times that references have caused wierd errors in my PHP code. Definitely a good recommendation.
narcissus
on Feb 28, 2013
|
next
[–]
I hear ya. In fact, I got burned by that problem enough times to make a test for it in my PHPCS 'coding standard'... which is basically a handful of standards that look for my stupid, repeated, coding errors :)
function_seven
on Feb 28, 2013
|
parent
|
next
[–]
My own standard is this:
foreach ($array as & $ref) { // Do something with $ref } unset ($ref);
i.e. put the `unset()` call on the same line as the closing brace, forever "welding" it to that block.
Kiro
on Feb 28, 2013
|
prev
[–]
Why do you even use references in the first place?
smsm42
on Feb 28, 2013
|
parent
[–]
Data structures like trees, etc. are pretty hard to do without them, due to default array by-value semantics.
Guidelines
|
FAQ
|
Lists
|
API
|
Security
|
Legal
|
Apply to YC
|
Contact
Search: