Bug #54931 [Com]: print_r does not reset() array pointer
| From: | roeitell at gmail dot com | Date: | Thu, 26 Apr 2012 20:42:44 +0000 |
| Subject: | Bug #54931 [Com]: print_r does not reset() array pointer | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-169980@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=54931&edit=1
ID: 54931
Comment by: roeitell at gmail dot com
Reported by: todd at magnifisites dot com
Summary: print_r does not reset() array pointer
Status: Open
Type: Bug
Package: Arrays related
Operating System: Any
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
IMO no harm in adding a defined state - on the contrary - and easily doable and
maintainable. We can commit to a stable defined state in the future, even for
users using the func for debugging. (debugging scripts, internal outputs /
reporting such as "nightly mail reports" etc.)
On zend.c#415, we can simply add --
...
print_hash(write_func, Z_ARRVAL_P(expr), indent, 0 TSRMLS_CC);
zend_hash_internal_pointer_reset(Z_ARRVAL_P(expr)); /* new line */
Z_ARRVAL_P(expr)->nApplyCount--;
...
Previous Comments:
------------------------------------------------------------------------
[2011-05-26 01:54:24] johannes@php.net
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.
------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=54931&edit=1