0

I'm coming to Php from Javascript and I can't understand why, to get, transform and return an array in Php, you have to use the "&" aka "reference" symbol in the foreach()...

       $some_array = array(1, 2, 3);



       function test($a) {
           foreach ($a as $k => &$v) {
               $v = 'Hello!';
           };
           return $a;
       };

       $result = test($some_array);


       var_dump($result); // array('Hello!', 'Hello!', 'Hello!');

But with:

 function test($a) {
           foreach ($a as $k => $v) {
               $v = 'Hello!';
           };
           return $a;
       };

[...]

var_dump($result); // array(1, 2, 3)
2
  • Another way, which may be easier to use for complex arrays is to set the original parameter array with the new value. You have the key of the current element. It is safe to do this in a foreach. see: eval.in/726978. i.e. it gives the same result without references. You need to know the element keys. Just a different way of getting the same effect. Commented Jan 30, 2017 at 15:46
  • Thanks @ryan-vincent, very clear answer! Commented Jan 30, 2017 at 15:53

2 Answers 2

3

To understand what "passed by value" and "passed by reference" are, you should read the docs:

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.

The docs on foreach construct also states:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

You may choose to pass by reference and change the value directly (&$v), or you may choose to pass by value, but in this case you have to change the array value using the element key in $k:

foreach ($a as $k => $v)
{
    $a[$k] = 'Hello!';
};
Sign up to request clarification or add additional context in comments.

4 Comments

Actually its nothing to do with the function argument in this case. It's purely to do with the reference used (or not used) on the foreach
@RiggsFolly dam right, I fixed my answer a bit, thanx
I dont really understand your last sentence either, I am not sure what you are saying there
@RiggsFolly is this better? :)
1

If you just pass the array to the foreach without using the reference &, you are passing a copy of the array into the foreach.

When you then make changes to the array (the copy) you make those changes to the copy. The copy of course is thrown away once the foreach finishes.

So when you return $a after the foreach has completed, you are actually returning the copy that was passed to the function as the parameter, which is unchanged.

Even if you were to pass the array, by reference, to the function it would make no difference as it is the foreach that either makes a disposable copy, or uses the original array, depending upon the use of the & reference instruction in the actual foreach code line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.