ID: 40610 Updated by: [EMAIL PROTECTED] Reported By: Webbed dot Pete at gmail dot com Status: Open -Bug Type: Arrays related +Bug Type: Documentation problem Operating System: Windows, Linux PHP Version: 5.2.1 New Comment:
Reclassified a docu problem. When you pass a non-existent variable by reference of course it HAS to be created, or what do you think should be referenced? Previous Comments: ------------------------------------------------------------------------ [2007-02-23 22:47:54] Webbed dot Pete at gmail dot com I have double and triple checked the documentation... http://us3.php.net/manual/en/language.references.whatdo.php ...tells us that <<<PHP references allow you to make two variables to refer to the same content. Meaning, when you do: <?php $a =& $b; ?> it means that $a and $b point to the same content. Note: $a and $b are completely equal here, that's not $a is pointing to $b or vice versa, that's $a and $b pointing to the same place.>>> This works as far as it goes. What's left unsaid is that if $b is a missing array key, the unset key will be created and the $b array will grow. Neither 'array' nor 'references' documentation specify this behavior. They don't say whether or not array elements are created willy-nilly when an array reference exists. I find nothing in the reference manual that either specifies nor suggests this behavior is intentional. I do find several user comments about this bug. It's just never been reported as a bug before. The impact is that we cannot trust the keys of an array. If this is declared as missing documentation, we will have to live with the bug. I'd prefer to see it eventually fixed; php will be better for it. ------------------------------------------------------------------------ [2007-02-23 20:13:24] [EMAIL PROTECTED] Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php ------------------------------------------------------------------------ [2007-02-23 19:01:44] Webbed dot Pete at gmail dot com Even simpler: $x=&$aEmpty['wheat']; Adds element 'wheat' to the array. This creates a nasty side effect for a couple of valuable and common functions, which basically extend isset() for default values and so forth: function varset(&$val,$default='') { if (isset($val)) return $val; return $default; } function varsettrue(&$val,$default='') { if (isset($val) && $val) return $val; return $default; } $myVal = varset($pref['maxsize'],1000); // set myVal to pref or default NOTE: all of the following leave $aEmpty alone. I understand why this might be the case, yet it still is wrong to break references IMHO, not least because of losing ability to create functions like those above. $aEmpty['wheat']; // simple reference isset($aEmpty['wheat']); // built-in function myFunc($aEmpty['wheat']); // pass-by-value to user func ------------------------------------------------------------------------ [2007-02-23 17:59:06] Webbed dot Pete at gmail dot com Description: ------------ Pass an unset array element by reference, without doing anything at all. The passed array will gain an element. (Note: We use isset() in our real function, which is what pointed us to the bug, but it is not necessary for demoing the defect.) Reproduce code: --------------- <?php // NOP function with pass-by-reference (PBR) parameter function refTest( &$param ) { } // Do test with empty and non-empty arrays $aEmpty = array(); $aOne = array( 'corn' ); // Initial state is fine echo "<pre>BEFORE\n"; echo "aEmpty contains ".count($aEmpty)." element(s)\n",print_r( $aEmpty, TRUE ); echo "aOne contains ".count($aOne)." element(s)\n",print_r( $aOne, TRUE ); // Pass by reference modifies the arrays. (we use with 'isset()' and saw this; I've reduced to basic issue.) $aEmpty = array(); $aOne = array( 'corn' ); refTest( $aEmpty['wheat'] ); refTest( $aOne['wheat'] ); echo "\nAFTER PASS BY REFERENCE\n"; echo "aEmpty contains ".count($aEmpty)." element(s)\n",print_r( $aEmpty, TRUE ); echo "aOne contains ".count($aOne)." element(s)\n",print_r( $aOne, TRUE ); ?> Expected result: ---------------- BEFORE aEmpty contains 0 element(s) Array ( ) aOne contains 1 element(s) Array ( [0] => corn ) [Same if you directly reference, or use isset() outside of a function, etc etc] [If you don't pass by reference you get notice error so that is not a solution] Actual result: -------------- AFTER PASS BY REFERENCE aEmpty contains 1 element(s) Array ( [wheat] => ) aOne contains 2 element(s) Array ( [0] => corn [wheat] => ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=40610&edit=1