From: Operating system: Mac OS X Snow Leopard PHP version: 5.3.5 Package: Arrays related Bug Type: Bug Bug description:Problem with passing array elements as references in foreach
Description: ------------ I created an array of strings that would be typecast to integers, and cast them using a foreach loop, passing each element by reference. After dumping the resulting array, it gave (somewhat) expected results, though after dumping each element individually, it gave an unexpected result on the last element. This happened with PHP 5.3.5 on Mac OS X Snow Leopard, but did not happen on a Debian system with PHP 5.2.6. Test script: --------------- <?php $array = array('1', '2', '5324', '435', '51'); foreach($array as &$element){ $element = (int)$element; } var_dump($array); foreach($array as $key => $element){ var_dump($key); var_dump($element); echo "\n"; } ?> Expected result: ---------------- array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5324) [4]=> int(435) [5]=> int(51) } int(0) int(1) int(1) int(2) int(2) int(3) int(3) int(5324) int(4) int(435) int(5) int(51) Actual result: -------------- array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5324) [4]=> int(435) [5]=> &int(51) } int(0) int(1) int(1) int(2) int(2) int(3) int(3) int(5324) int(4) int(435) int(5) int(435) -- Edit bug report at http://bugs.php.net/bug.php?id=53870&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=53870&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=53870&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=53870&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=53870&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53870&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=53870&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=53870&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=53870&r=needscript Try newer version: http://bugs.php.net/fix.php?id=53870&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=53870&r=support Expected behavior: http://bugs.php.net/fix.php?id=53870&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=53870&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=53870&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=53870&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53870&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=53870&r=dst IIS Stability: http://bugs.php.net/fix.php?id=53870&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=53870&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=53870&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=53870&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=53870&r=mysqlcfg