note 81297 added to control-structures.for

From: Date: Thu, 21 Feb 2008 16:46:07 +0000
Subject: note 81297 added to control-structures.for
Groups: php.notes 
Request: Send a blank email to php-notes+get-136358@lists.php.net to get a copy of this message
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. ---- Server IP: 64.71.164.2 Probable Submitter: 66.186.116.126 ---- Manual Page -- http://www.php.net/manual/en/control-structures.for.php Edit -- https://master.php.net/note/edit/81297 Del: integrated -- https://master.php.net/note/delete/81297/integrated Del: useless -- https://master.php.net/note/delete/81297/useless Del: bad code -- https://master.php.net/note/delete/81297/bad+code Del: spam -- https://master.php.net/note/delete/81297/spam Del: non-english -- https://master.php.net/note/delete/81297/non-english Del: in docs -- https://master.php.net/note/delete/81297/in+docs Del: other reasons-- https://master.php.net/note/delete/81297 Reject -- https://master.php.net/note/reject/81297 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136358) next »