From: Webbed dot Pete at gmail dot com
Operating system: Windows, Linux
PHP version: 5.2.1
PHP Bug Type: Arrays related
Bug description: Side effect of passing array element by reference
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 bug report at http://bugs.php.net/?id=40610&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=40610&r=trysnapshot44
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=40610&r=trysnapshot52
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=40610&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=40610&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=40610&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=40610&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=40610&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=40610&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=40610&r=support
Expected behavior: http://bugs.php.net/fix.php?id=40610&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=40610&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=40610&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=40610&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40610&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=40610&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=40610&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=40610&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=40610&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=40610&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=40610&r=mysqlcfg