note 71259 rejected from function.current by tularis

From: Date: Mon, 20 Nov 2006 00:00:30 +0000
Subject: note 71259 rejected from function.current by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-119881@lists.php.net to get a copy of this message
Note Submitter: randallgirard at hotmail dot com ---- 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 :-/

« previous php.notes (#119881) next »