ID: 40610 User updated by: Webbed dot Pete at gmail dot com Reported By: Webbed dot Pete at gmail dot com Status: Open Bug Type: Arrays related Operating System: Windows, Linux PHP Version: 5.2.1 New Comment:
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 Previous Comments: ------------------------------------------------------------------------ [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