3

I know that scope works differently in PHP and in Javascript.

When I first started learning Javascript (after a couple of years learning PHP), I did not initially realise that variables declared outside a function were also accessible from inside the function.

Now (after a few years of focusing much more on Javascript), I am stumped by how to return a PHP function-scope variable back to the extra-function environment.

Example:

$myArray = array();

function addItemsToMyArray($myArray) {

    $myArray[] = 'apple';
    $myArray[] = 'banana';
    $myArray[] = 'coconut';

    return $myArray;
}  

addItemsToMyArray($myArray);

echo count($myArray);    /* Gives 0 */

Why does count($myArray) give 0 instead of 3?

3
  • 1
    You need to write function addItemsToMyArray(&$myArray) Commented Nov 3, 2017 at 13:19
  • 1
    or make use of the returned value: $myArray = addItemsToMyArray($myArray); Commented Nov 3, 2017 at 13:20
  • 1
    Because the parameter of your function is local to your function and don't modify the outer scope of your function. If you add &, it tells PHP to write into $myArray globally, not locally. That's how I understand it anyway. Maybe someone can give a more detailled answer? Or more correct? Edit: @AlexBlex is right also Commented Nov 3, 2017 at 13:21

2 Answers 2

2

The function addItemsToMyArray() has correctly been set up to return the array to the main PHP code but you forgot to catch that return value and put it in a variable. One way to write this code and make the difference easier to see could be like this:

function addItemsToMyArray($tmpArray) {

    $tmpArray[] = 'apple';
    $tmpArray[] = 'banana';
    $tmpArray[] = 'coconut';

    return $tmpArray;
}  

$myArray = array();
$myArray = addItemsToMyArray($myArray);

echo count($myArray);    /* Gives 3 */

The variable used inside the function is a different variable than the $myArray variable outside of the function.

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

1 Comment

Brilliant. Thank you, @Tweek, for the clear explanation. I see that the difference between javascript and PHP in this situation is that because PHP passes variables to functions by value (not by reference) , the line $myArray = addItemsToMyArray($myArray); must be explicitly stated if the value of $myArray is to update.
2

Unless you specify otherwise, function/method arguments are pass-by-value, meaning you get a copy of them inside the function. If you want the function to change the variable that was passed to it, you need to pass-by-reference. As described here:

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:

Note the ampersand before $array in the doc page for sorting functions like asort() and ksort():

bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

2 Comments

This is a clear explanation, helping me to better understand the underlying mechanics in PHP - thank you @AlexHowansky. Would it then be correct to say that in function myFunction(myVariable) in Javascript, myVariable is passed by reference whereas in function My_Function($My_Variable) in PHP, $My_Variable is passed by value?
I believe JS is pass-by-reference for objects and pass-by-value for everything else.

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.