Quick PHP Tip: passing by reference creates array keys
Posted on 29 April 2009 (06:35 PM)
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:
$a = array('a' => 1, 'b' => 2);
$c = &$a['c'];
- Download this code: /code/passing-by-reference-creates-array-keys1.txt
Shockingly, this won't give you a notice or warning. Instead, it'll output this array:
[a] => 1
[b] => 2
- Download this code: /code/passing-by-reference-creates-array-keys2.txt
Filed under PHP
- ← previous article: Quick PHP Tip: close your connection between stream_get_contents calls
- → next article: Move current selection to new folder with Applescript
Sorry, due to spam, comments are temporarily out of order.