php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #7578 next() and current() do not return referenceing arrays
Submitted: 2000-11-01 19:29 UTC Modified: 2010-12-03 17:13 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mog at linux dot nu Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.0.1pl2 OS: RedHat Linux 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mog at linux dot nu
New email:
PHP Version: OS:

 

 [2000-11-01 19:29 UTC] mog at linux dot nu
i hope you can see what is wrong in the code below

<?PHP
$array2 = array(0,1,2);
$array = array(&$array2);

print current(current($array))."<br>"; // returns 0

print "<b>Try 1!</b><br>";
print next(current($array))."<br>"; // returns 1, correct but the internal pointer is only moved in the copy current() returned
print current(current($array))."<br>"; // returns 0, wrong, should be 1
print current($array2)."<br>"; //returns 0, wrong, should be 1

print "<b>Try 2!</b><br>";
print next($array[0])."<br>";
print current(current($array))."<br>"; // returns 0, wrong, should be 1
print current($array[0])."<br>"; // returns 1, yes, correct! but the code above still didn't work!
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-11-01 19:43 UTC] waldschrott@php.net
you simply can?t do it that way, please read the manual on foreach() etc. and that additional "&" in 
> $array = array(&$array2);
has absolutely no effect here, at least not what you expect it to do (please also read "references explained")

> print next(current($array))."<br>"; // returns 1, correct > but the internal
> pointer is only moved in the copy current() returned

sure, current($array) returns a copy and thus all results you?ve mentioned are fine
 [2000-11-02 22:12 UTC] mog at linux dot nu
1. yes, i have read the manual on references explained.

2. ok, array(&$array2) might not work but i can't see why.

what if we make it like this? shouldn't this work
or it should work like this if current(), reset(), prev() and next() were modified to return references instead.

<?PHP
$array = array(array(0,1,2));

print current(current($array))."<br>"; // returns 0

print "<b>Try 1!</b><br>";
print next(current($array))."<br>"; // returns 1, correct but the internal pointer is only moved in the copy current() returned
print current(current($array))."<br>"; // returns 0, wrong, should be 1
print current($array[0])."<br>"; //returns 0, wrong, should be 1

print "<b>Try 2!</b><br>";
print next($array[0])."<br>";
print current(current($array))."<br>"; // returns 0, wrong, should be 1
print current($array[0])."<br>"; // returns 1, yes, correct! but the code above still didn't work!
?>

I really think that this behaviour should be changed, i need it for a project i'm working with.

 [2000-11-05 06:19 UTC] stas@php.net
There's no & operation in PHP. Thus, you cannot use it in expressions, including array(). There's =& operator and &$var syntax for passing variables by reference.

Also, if language doesn't do what you need for a particular project, it's usually not good enough reason to change the language. Anyway, I move it to feature requests.
 [2009-12-14 17:07 UTC] zhalassy at loginet dot hu
This file is rather old (9 years? wow!)...

What i can't do in PHP 5.2 currently is:

$a = array(2,5,8);
$e =& end($a);

I have to do this instead:

$a = array(2,5,8);
end($a);
$e =& $a[key($a)]; /* Overhead with an extra function call, and a key lookup */

It would be nice if current(), reset(), end(), next() and prev() would return references at least...
 [2010-12-03 17:13 UTC] jani@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2010-12-03 17:13 UTC] jani@php.net
This is how it works. No way we should even consider changing it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC