php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74724 current returns false but each returns value
Submitted: 2017-06-09 06:07 UTC Modified: 2020-11-23 16:07 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: tell dot a dot name dot to dot me at gmail dot com Assigned:
Status: Verified Package: Variables related
PHP Version: 7.1.6 OS: win/linux
Private report: No CVE-ID: None
 [2017-06-09 06:07 UTC] tell dot a dot name dot to dot me at gmail dot com
Description:
------------
<?php
$arr = ['a', 'b', 'c'];
var_dump(current($arr));
echo '--------aaaaaa--------', PHP_EOL;
var_dump(end($arr));
var_dump(next($arr));

$abc = $arr;

echo '--------bbbbbbb--------', PHP_EOL;

var_dump(current($abc)); // false
var_dump(current($abc)); // false
var_dump(each($abc)); // not false???
var_dump(each($abc)); // not false???
var_dump(each($abc)); // not false???
echo '--------cccccccc--------', PHP_EOL;

Test script:
---------------
<?php
$arr = ['a', 'b', 'c'];
var_dump(current($arr));
echo '--------aaaaaa--------', PHP_EOL;
var_dump(end($arr));
var_dump(next($arr));

$abc = $arr;

echo '--------bbbbbbb--------', PHP_EOL;

var_dump(current($abc)); // false
var_dump(current($abc)); // false
var_dump(each($abc)); // not false???
var_dump(each($abc)); // not false???
var_dump(each($abc)); // not false???
echo '--------cccccccc--------', PHP_EOL;


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-09 08:00 UTC] sjon at hortensius dot net
Looking at https://3v4l.org/dGZbf the reporter considers the difference between php5 & php7 to be a bug

However, I think this is caused by the changes made for https://wiki.php.net/rfc/php7_foreach The documentation could be updated; but each is already marked as deprecated
 [2017-06-09 08:21 UTC] requinix@php.net
-Summary: each bug? +Summary: current returns false but each returns value -Package: *General Issues +Package: Variables related
 [2017-06-09 08:21 UTC] requinix@php.net
The point is that current($abc) returns false but each($abc) returns a value. It's inconsistent: either the pointer is reused and both return false, or the pointer is reset for the new variable and both return values.
 [2020-11-23 16:07 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2020-11-23 16:07 UTC] cmb@php.net
The culprit is that we're resetting the internal pointer if it is
beyond the valid range when duplicating the array[1].

[1] <https://github.com/php/php-src/blob/php-7.4.12/Zend/zend_hash.c#L2080-L2082>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 10 07:01:28 2024 UTC