note 81297 deleted from control-structures.for by danbrown

From: Date: Mon, 07 Dec 2009 15:58:21 +0000
Subject: note 81297 deleted from control-structures.for by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163641@lists.php.net to get a copy of this message
Note Submitter: Lawrence ---- PHP does not support a 'redo' statement that is common in other languages (such as Perl). However, PHP does not throw an error if it encounters the bareword 'redo'. The following code executes without an error and without 'redoing' anything: for ($i = 0; $i < 3; $i++) { print "I: $i\n"; if ($count++ < 2) { print " count: $count\n"; redo; } print " past redo\n"; } In Perl, this would output: I: 0 count: 1 I: 0 count: 2 I: 0 past redo I: 1 past redo I: 2 past redo In PHP, this outputs: I: 0 count: 1 past redo I: 1 count: 2 past redo I: 2 past redo PHP sees the 'redo;' line as an unquoted string and silently passes over it with no error (unless you have E_ALL error reporting turned on). To get a close approximation to a 'redo' statement, you have to replace the redo with something like $i--; continue; as in: for ($i = 0; $i < 3; $i++) { print "I: $i\n"; if ($count++ < 2) { print " count: $count\n"; $i--; continue; } print " past redo\n"; } The '$i--;' part has to reverse the effects of the third part of the for statement -- in this case the '$i++' -- as the third part will still be executed in PHP. Note also that the test part of the for statement will also be reevaluated (unlike a proper 'redo' which doesn't), so any side-effects in that part of the for statement will also be applied.

« previous php.notes (#163641) next »