Quick PHP Tip: passing by reference creates array keys

Another little PHP tip that could save you a couple of debugging hours.


Consider the following code, and notice I use the ampersand character to fetch the value by reference:

<?php $a = array('a' =?> 1, 'b' => 2);
	$c = &$a['c'];
	print_r($a);
?>

Shockingly, this won’t give you a notice or warning. Instead, it’ll output this array:

Array
(
    [a] => 1
    [b] => 2
    [c] => 
)

This might have disastrous results, if your code works with those array keys one way or another. So be warned, and learn to love functions such as array_key_exists and empty.