New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Comments
aww what have you got against ampersands?
If you mean the specific bit of code in the OP then I will say the readability of it could be improved. As an example see this bit in the WP coding guidelines:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#clever-code
Is English not your first language? You really need to re-read this thread if you think the complaint is about the ampersand symbol... (Which is the indicator for calling by reference in a lot of other languages too)
No it isn't. Can you spell it out for me coz I'm not sure what you mean.
Sorry, I stand corrected. I did read it wrong and having looked into it, looking at the code does not clearly tell what the script does unless you know how references work in PHP.
In this case I think it would be logic if the reference would be unset automatically as to me it seems the intention of the programmer is to just set it for the loop. However, that's not what PHP does.
That's what I figured. The script above looks like it should output
Thing 1
Thing 2
Thing 3
And it would in just about every other language, but in PHP you need to unset the reference variable after the foreach loop or you get:
Thing 1
Thing 2
Thing 2
The behavior of the code is a little misleading. If you want to know why, just re-read the thread and all the details are there explaining what the problem is and why it happens.
At the end of the script, $things[2]['name'] and $copythings[2]['name'] are both 'Thing 2' instead of 'Thing 3'.
I didn't notice that. There is a warning about it in the manual:
http://php.net/manual/en/control-structures.foreach.php
Quite the edge case that.
This was stated like.. fourth response down.. next time read the responses first... That's how a forum works.
That being said, not really an edge case. Lots of scenarios where you would want to foreach loop through an array and modify the original values.