note 71259 added to function.current
| From: | randallgirard at hotmail dot com | Date: | Sun, 19 Nov 2006 22:03:53 +0000 |
| Subject: | note 71259 added to function.current | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119879@lists.php.net to get a copy of this message | ||
I have found a very strange bug in PHP. Long story short, I was coding some advanced subroutine
looping for an SQL Query generator I've been working on, and well, I found a bug when using
references with KEY and CURRENT
$arr = array('0', '1', '2', '3');
reset($arr);
print('Direct ');
print('NEXT: '.next($arr).' ');
print('NEXT: '.next($arr).' ');
print('NEXT: '.next($arr).' ');
print('KEY: '.key($arr).' ');
print('CURRENT: '.current($arr).' ');
print("<br><br>\n");
$ref =& $arr;
reset($ref);
print('Referended ');
print('NEXT: '.next($ref).' ');
print('NEXT: '.next($ref).' ');
print('NEXT: '.next($ref).' ');
print('KEY: '.key($ref).' ');
print('CURRENT: '.current($ref).' ');
print("<br><br>\n");
Your output should be like:
Direct NEXT: 1 NEXT: 2 NEXT: 3 KEY: 3 CURRENT: 3
Referended NEXT: 1 NEXT: 2 NEXT: 3 KEY: 0 CURRENT: 0
But now, notice this next code (I moved the reference asignment up before the first print set)
$arr = array('0', '1', '2', '3');
$ref =& $arr;
reset($arr);
print('Direct ');
print('NEXT: '.next($arr).' ');
print('NEXT: '.next($arr).' ');
print('NEXT: '.next($arr).' ');
print('KEY: '.key($arr).' ');
print('CURRENT: '.current($arr).' ');
print("<br><br>\n");
reset($ref);
print('Referended ');
print('NEXT: '.next($ref).' ');
print('NEXT: '.next($ref).' ');
print('NEXT: '.next($ref).' ');
print('KEY: '.key($ref).' ');
print('CURRENT: '.current($ref).' ');
print("<br><br>\n");
Jee Golly! AHHHH! Now our output is:
Direct NEXT: 1 NEXT: 2 NEXT: 3 KEY: 0 CURRENT: 0
Referended NEXT: 1 NEXT: 2 NEXT: 3 KEY: 0 CURRENT: 0
So it seems that once an array is referenced elsewhere, KEY and CURRENT just well, stop working all
together. I really shouldn't think this should happen, as NEXT, PREV, etc still seem to work as
expected.
I say, everyone should use a real scripting engine, hence: Perl ;D PHP just lacks in WAY too many
areas, and I constantly keep finding bug after bug...... But eh, it's what many of my clients
want so I'm stuck :-/
----
Server IP: 216.194.113.175
Probable Submitter: 71.119.235.161
----
Manual Page -- http://www.php.net/manual/en/function.current.php
Edit -- https://master.php.net/note/edit/71259
Del: integrated -- https://master.php.net/note/delete/71259/integrated
Del: useless -- https://master.php.net/note/delete/71259/useless
Del: bad code -- https://master.php.net/note/delete/71259/bad+code
Del: spam -- https://master.php.net/note/delete/71259/spam
Del: non-english -- https://master.php.net/note/delete/71259/non-english
Del: in docs -- https://master.php.net/note/delete/71259/in+docs
Del: other reasons-- https://master.php.net/note/delete/71259
Reject -- https://master.php.net/note/reject/71259
Search -- https://master.php.net/manage/user-notes.php