1

I am trying to understand the following code in PHP. Here is the overview,

There are 2 arrays, which consists of key values pairs in the form, $k => $v

These 2 arrays are merged together using array_merge function to form the third array.

Now, this array is passed to a function. Only one argument, the array name is passed.

Here is the code (please note that this code is only a concept, not the real code):

<?php    

function test(&$myArray, 0)
{
    reset($myArray);

    foreach ($myArray as $k => $v)
    {
      ....
    }
}

$arr3 = array_merge((array) $arr1, (array) $arr2);

test($arr3)

}

Questions:

  1. The function is defined in such a way, that it expects two arguments, however we are passing only one argument. Is it because the second argument is always 0 as initialized in the function prototype? So, there is no need to pass the same number of arguments?
  2. Here the array name is passed. My guess is that, it will pass a pointer to the first element of the array (base address of the array in memory) to the function.

In this case, if you look at the prototype, the array name is preceded with an ampersand. So, this means, a reference to the array, the address?

  1. Why is it necessary to call the reset function on the array. Is it because the array_merge function which was used to form this array was called before the test function? So, it moved the pointer in the array ahead of the first element as a result of merging $arr1 and $arr2?

  2. There is no value returned in the function, test. So, does it modify the value of the original array in the memory itself, and hence there is no need to return an array?

Thanks.

5
  • is this a question for an assignment or homework or something? Commented Sep 20, 2012 at 6:16
  • 1
    This code doesn't work; you're missing the sigils (&$myArray), you've got a 0 in your function parameter list, and you have a mysterious trailing brace. Commented Sep 20, 2012 at 6:16
  • This is only a conceptual code (not the real code). It is only to understand the concept. @TheShiftExchange: No, this is not an assignment. I was reading the source code of a PHP File and came across a similar code, so I wanted to understand. Thanks. Commented Sep 20, 2012 at 7:54
  • Your concept code does not make any sense. Could you post something that works in some way or has at least some meaningful interpretation? Commented Sep 20, 2012 at 7:54
  • It does make sense as long as you focus on the concepts I am trying to understand. You can pick any array initialized with key value pairs as an example and assign it to $arr3. It is an incomplete code, true, but yes, it does make sense. Infact, Angelin seems to have understood my question quite well. Commented Sep 20, 2012 at 8:14

1 Answer 1

1
<?php
function test($myArray,$a =0) //passing by value
{
    reset($myArray);
    return $myArray;         
}

$arr3 = test($arr3); //call n store it back it in $arr3

function test(&$myArray,$a =0) //passing by reference
    {
        reset($myArray);

    }
test($arr3);  //just call;

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Angelin, thank you for the code example. So, my understanding is correct. When we pass it as a reference, there is no need to return, as the original array is modified (same concept as in C). Also, can you clarify the point 1 in my question. You have the second argument of the test function as $a=0, but while calling the function, we are passing only one argument, why is it so?
Ohh its nothing but way of defining the default value for second argument and saying its opitonal and if u pass a 2nd arg the default value will be overwritten.So, its not compulsory to call test() by 2 args.
Thank you so much :) Greetings from India too ;)

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.