Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


PHP references - Page 2
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.

PHP references

2»

Comments

  • AbdussamadAbdussamad Member
    edited August 2016

    Jonchun said: He's not talking about the feature.. He's talking about the implementation.

    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

  • @Abdussamad said:

    Jonchun said: He's not talking about the feature.. He's talking about the implementation.

    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)

  • Jonchun said: Is English not your first language?

    No it isn't. Can you spell it out for me coz I'm not sure what you mean.

  • @mpkossen said:

    perennate said: But regardless, could you tell what the script would print before running it?

    Yes, actually. This is how PHP has worked for ages.

    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.

  • JonchunJonchun Member
    edited August 2016

    @Abdussamad said:

    Jonchun said: Is English not your first language?

    No it isn't. Can you spell it out for me coz I'm not sure what you mean.

    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.

  • perennateperennate Member, Host Rep

    Abdussamad said: No it isn't. Can you spell it out for me coz I'm not sure what you mean.

    At the end of the script, $things[2]['name'] and $copythings[2]['name'] are both 'Thing 2' instead of 'Thing 3'.

    Thanked by 1Jonchun
  • perennate said: 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:

    Warning

    Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset(). Otherwise you will experience the following behavior:

    http://php.net/manual/en/control-structures.foreach.php

    Quite the edge case that.

  • @Abdussamad said:

    perennate said: 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:

    Warning

    Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset(). Otherwise you will experience the following behavior:

    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.

Sign In or Register to comment.