The question was what's gonna be printed when the code below is executed?
My answer was:
arr[0]=1 and arr2[0]=2
But apparently, no. As seen during execution, the answer appears to be arr[0]=2 and arr2[0]=2. Now, that confuses me - why would arr[0]
be 2, if it wasn't even referenced, and as such shouldn't be changed when arr2[0]
was modified?
$a = 1;
$arr = array(1);
$a = &$arr[0]; //$a=1
$arr2 = $arr;
$arr2[0]++; //$arr2[0]=2
echo "arr[0]=".$arr[0]. "<br>";
echo "arr2[0]=".$arr2[0]. "<br>";
I'm probably missing something embarrassingly obvious, but can't seem to figure it out. Thanks, in advance!