Skip to content

Instantly share code, notes, and snippets.

@meglio
Created September 6, 2011 16:12
Show Gist options
  • Save meglio/1198014 to your computer and use it in GitHub Desktop.
Save meglio/1198014 to your computer and use it in GitHub Desktop.
Unexpected behavior of arrays with referenced elements, in PHP
<?php
$arr = array(1, 5);
$a =& $arr[0]; //$a and $arr[0] are in the same reference set
$arr2 = $arr; //not an assignment-by-reference!
$arr2[0]++;
$arr2[1]++;
var_dump($a);
var_dump($arr);
// Result is unexpected:
// int(2) array(2) { [0]=> &int(2) [1]=> int(5) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment