note 121605 deleted from control-structures.goto by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:42:38 +0000 |
| Subject: | note 121605 deleted from control-structures.goto by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215280@lists.php.net to get a copy of this message | ||
Note Submitter: Mark
----
Here are two examples of how GOTO can simplify code and actually make it more readable and easier to
understand.
1. GOTO can be used to exit an IF block:
<?php
if(something) {
if(nothing) goto endif;
// blah blah
// blah blah
endif:
// Nothing here, but maybe some cleanup
}
?>
2. GOTO can be used to jump ahead to a section of code. This can be useful for trouble shooting or
testing a script in small stages.
<?php
goto block3;
block1:
// blah blah
block2:
// blah blah
block3:
// blah blah
?>
Note that in both cases, I have gone forward, not backward. This makes it more manageable.
In the case of the IF block, many structures, such as a loop, a switch or a function, have an early
exit, such as break or return. IF doesnât, so this is a workable solution