note 101171 added to control-structures.goto
| From: | mewsterus at gmail dot com | Date: | Tue, 30 Nov 2010 21:49:40 +0000 |
| Subject: | note 101171 added to control-structures.goto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174498@lists.php.net to get a copy of this message | ||
@tweston: Your code actually mimics a do {} while loop, not a plain while {} loop. This is what you
were looking for:
<?php
$start = microtime(true);
$i = 0;
StartOfLoop:
if(!($i < 1000000)) goto EndOfLoop;
$i++;
goto StartOfLoop;
EndOfLoop:
echo microtime(true) - $start.PHP_EOL;
?>
And of course it's as fast as a while loop. It might even be faster. Every single control
structure except for the basic "if" (even "else") is ultimately implemented as
GOTO statements. PHP probably doesn't do this, it probably passes the control structures onto
C, but the C compiler translates all of the control statements to GOTO statements. The reason it is
bad coding practice to do this manually is because while a compiler only has to define the labels
once and stick with it, as humans writing ever-changing computer code, we will make mistakes in the
future when modifying the code. It's less likely we'll make a mistake with well-defined
control structures than with GOTO statements, especially with the older languages and machine code
where it might be defined as a code offset rather than a label.
----
Server IP: 208.69.120.55
Probable Submitter: 129.186.122.32
----
Manual Page -- http://www.php.net/manual/en/control-structures.goto.php
Edit -- https://master.php.net/note/edit/101171
Del: integrated -- https://master.php.net/note/delete/101171/integrated
Del: useless -- https://master.php.net/note/delete/101171/useless
Del: bad code -- https://master.php.net/note/delete/101171/bad+code
Del: spam -- https://master.php.net/note/delete/101171/spam
Del: non-english -- https://master.php.net/note/delete/101171/non-english
Del: in docs -- https://master.php.net/note/delete/101171/in+docs
Del: other reasons-- https://master.php.net/note/delete/101171
Reject -- https://master.php.net/note/reject/101171
Search -- https://master.php.net/manage/user-notes.php