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?
function addItemsToMyArray(&$myArray)$myArray = addItemsToMyArray($myArray);&, 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