$value versus &$value in foreach

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andrewteg
    New Member
    • Aug 2007
    • 22

    $value versus &$value in foreach

    What is the difference between these two lines of code. I know in the second I added a & but what does that do exactly?

    [PHP]foreach ($arr as $value)[/PHP]

    [PHP]foreach ($arr as &$value)[/PHP]

    Thanks,
    Andrew
  • theS70RM
    New Member
    • Jul 2007
    • 107

    #2
    Originally posted by andrewteg
    What is the difference between these two lines of code. I know in the second I added a & but what does that do exactly?

    [PHP]foreach ($arr as $value)[/PHP]

    [PHP]foreach ($arr as &$value)[/PHP]

    Thanks,
    Andrew

    is the difference that one of them creates a parse error? =P

    as far as i know (and i am no expert) the & does nothing and is incorrect syntax.


    Andy

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by theS70RM
      is the difference that one of them creates a parse error? =P

      as far as i know (and i am no expert) the & does nothing and is incorrect syntax.


      Andy
      I'm not sure; i've seen it used in other scripts.

      Never known what it was for though.

      Be useful to get some insight though :)

      Regards.

      Comment

      • theS70RM
        New Member
        • Jul 2007
        • 107

        #4
        check this




        an addition to php5 it would seem.

        pointers, references etc just confuse me, thats why i leave C alone ;)


        Andy

        Comment

        • andrewteg
          New Member
          • Aug 2007
          • 22

          #5
          I know they are both valid. I did a foreach with the & and it changed the value of my variable but without it it did not!

          I have seen the code on www.php.net and actually just found this on http://www.php.net/foreach
          As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.
          So it's a PHP5 thing that passes by reference and can modify your variable.

          Uses & so changes $arr
          [PHP]
          $arr = array(1, 2, 3, 4);
          foreach ($arr as &$value) {
          $value = $value * 2;
          }
          // $arr is now array(2, 4, 6, 8)
          [/PHP]
          No & so no change to $arr
          [PHP]
          $arr = array(1, 2, 3, 4);
          foreach ($arr as $value) {
          $value = $value * 2;
          }
          // $arr is still array(1, 2, 3, 4)
          [/PHP]

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by andrewteg
            I know they are both valid. I did a foreach with the & and it changed the value of my variable but without it it did not!

            I have seen the code on www.php.net and actually just found this on http://www.php.net/foreach

            So it's a PHP5 thing that passes by reference and can modify your variable.

            Uses & so changes $arr
            [PHP]
            $arr = array(1, 2, 3, 4);
            foreach ($arr as &$value) {
            $value = $value * 2;
            }
            // $arr is now array(2, 4, 6, 8)
            [/PHP]
            No & so no change to $arr
            [PHP]
            $arr = array(1, 2, 3, 4);
            foreach ($arr as $value) {
            $value = $value * 2;
            }
            // $arr is still array(1, 2, 3, 4)
            [/PHP]
            Ahhhh, useful!

            Regards

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Good question. I didn't even know the "FOREACH construct used to loop through PHP arrays works on a COPY of the array and not the array itself" so I will definately be using this in the future.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                For your information: there is quite a good explanation about passing values by pointer and by reference in the PHP documentation in chapter References Explained

                Ronald

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by TheServant
                  Good question. I didn't even know the "FOREACH construct used to loop through PHP arrays works on a COPY of the array and not the array itself" so I will definately be using this in the future.
                  Be careful tho.
                  As explained in one of the links posted above, even tho the old un-referenced foreach is in fact working on a copy of the array, the internal PHP engine still references the original array until you actually modify the copy.

                  In the end, referencing the array will trigger internal safe-guards that may cause your code to run slower.

                  So, only use the referenced version if you plan on actually changing the original data while iterating through it.

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Originally posted by Atli
                    Be careful tho.
                    As explained in one of the links posted above, even tho the old un-referenced foreach is in fact working on a copy of the array, the internal PHP engine still references the original array until you actually modify the copy.

                    In the end, referencing the array will trigger internal safe-guards that may cause your code to run slower.

                    So, only use the referenced version if you plan on actually changing the original data while iterating through it.
                    Good point, I will definately do that. Thanks.

                    Comment

                    Working...