Tricky each() on expression instead of variable

From: Date: Fri, 18 Aug 2000 22:58:37 +0000
Subject: Tricky each() on expression instead of variable
Groups: php.general 
Request: Send a blank email to php-general+get-12545@lists.php.net to get a copy of this message
I just wasted about an hour finding this bug in my program, which I at first blamed the PHP engine. This post to illuminate y'all of the sometimes tricky workings of programming languages in the late hours. Imagine a construct using each() on an expression: while ( list(, $v) = each( split(",", f() ) ) ) <some code> Where f() returns some string like "4,5,6". This hangs in an endless loop. I think it has something to do with the program evaluating the expression (split(...)) in each iteration of the while-loop, hence wiping the state variable that the each() function uses. So I figured this is actually a bug in my program rather than the PHP engine like I first assumed. Of course the correct way to deal with this is to simply use a temporary variable to hold the array: $id_list = split(",", f() ); while ( list(, $v) = each( $id_list ) ) <some code> Looking back I'm ashamed of my programming efforts. :o) Cheers, -- Mikkel R. Jakobsen -- mikkelj@unigo.dk "The intelligence on this planet is constant. The population is growing."

« previous php.general (#12545) next »