Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created May 30, 2014 20:48
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 adamloving/f0d8328d9451ccfc45ae to your computer and use it in GitHub Desktop.
Save adamloving/f0d8328d9451ccfc45ae to your computer and use it in GitHub Desktop.
Daily PHP WTF. Adding a key to an array creates a new and different array.
<?php
$a = array();
$b = $a;
// "When using the identity operator (===), object variables are identical
// if and only if they refer to the same instance of the same class."
if ($a === $b) print "same\n";
$a['test'] = 'something';
if ($a === $b)
print "same\n";
else
print "No longer the same object reference\n"; // Guess what, $a doesn't point to $b any more
// Let's try that again
$a = array();
$b = &$a; // use ampersand to explicitly make reference a pointer
if ($a === $b) print "same\n";
$a['test'] = 'something';
if ($a === $b)
print "same\n"; // now they point to the same thing!
else
print "No longer the same object reference\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment