Edit report at http://bugs.php.net/bug.php?id=54931&edit=1
ID: 54931 Updated by: johan...@php.net Reported by: todd at magnifisites dot com Summary: print_r does not reset() array pointer -Status: Open +Status: To be documented Type: Bug Package: Arrays related Operating System: Any PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: As print_r is aimed as a debugging help we should document this as "undefined" behavior so we can change it any time. Users should still be advised to use reset() if they want a defined state. Previous Comments: ------------------------------------------------------------------------ [2011-05-25 23:27:57] todd at magnifisites dot com Description: ------------ The PHP documentation states: Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning. But after using print_r() on an array, the array pointer is not reset. This is the same as Bug #8289 from PHP 4 in the year 2000: http://bugs.php.net/bug.php?id=8289 Test script: --------------- $arr['a'] = 'Apple'; $arr['b'] = 'Banana'; $arr['c'] = 'Corn'; print_r($arr); //reset($arr); while(list($k,$v) = each($arr)) echo "$k => $v\n"; Expected result: ---------------- Since print_r moves the array pointer to the end I would expect FALSE to be returned by each() and nothing to be printed. However, I also note that foreach() has some side effects so perhaps this is not necessarily a bug but a documentation issue? foreach() notes: http://www.php.net/manual/en/control-structures.foreach.php Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it. Actual result: -------------- Array ( [a] => Apple [b] => Banana [c] => Corn ) a => Apple b => Banana c => Corn ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54931&edit=1