Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active March 25, 2017 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/911f10fca3f7e689cb1fc4b52b56a5e9 to your computer and use it in GitHub Desktop.
Save Noitidart/911f10fca3f7e689cb1fc4b52b56a5e9 to your computer and use it in GitHub Desktop.
_php-array-merge-ref-preservation - Shows that references are presereved when merging arrays with +=
<?php
$foo = 12;
$a1 = ['foo' => &$foo];
$showa1 = function() use(&$a1) {
print_r($a1);
};
$bar = 100;
$a2 = ['bar' => &$bar];
// $merged = array_merge($a1, $a2);
$a1 += $a2;
// $a1['foo'] = 24;
// $a1['bar'] = 9;
// print assert($foo === 24); // works just fine
// print assert($bar === 9); // works just fine
$a1['foo'] = 24;
$bar = 9;
print assert($foo === 24); // works just fine
print assert($a1['bar'] === 9); // works just fine
$showa1();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment