PHP passes array by value unless you declare function signature to take reference

yep, calling the function below will not have changed the array after the call

public function addMore(array $names) {
   $names[] = 'another';
   return;
}

..you have to declare the function signature to take a reference to the array, then the array will be changed after the call


public function addMore(array &$names) {
   $names[] = 'another';
   return;
}

Objects passed can be modified without the need to declare as references in the signature as they are passed by reference by default;

Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more