note 123478 deleted from control-structures.goto by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:42:41 +0000 |
| Subject: | note 123478 deleted from control-structures.goto by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215281@lists.php.net to get a copy of this message | ||
Note Submitter: romeu at gamelas dot pt
----
This function despite it's bad name is pretty useful and the most CPU effective in to control a
flow of multiple cascading situations.
Let's say you have some form set, but before it you need to perform a lot of checks on which
form to show based upon user permissions or settings:
<?php
$user_info = ...; //something you get before from session, cookies, database...
if($user_info != "already_set"){
//Some setting need isn't yet done
loadForm("form1");
}else{
//OK, the set was done, so let's continue
if($user_info != "some_other_thing"){
//Another thing missing...
loadForm("form2");
}else{
//OK, the second part is done, let's do the third...
if($user_info != "some_yet_another_thing"){
//And yet another thing to setup...
loadForm("form3");
}else{
/* Guess you can see the picture now, it will be if(){ else if(){ else if(){ else....}}}}}}}}
}
}
}
?>
Goto can handle this situation like:
<?php
if($user_info != "already_set") {
loadForm("form1");
goto move_on;
}
if($user_info != "some_other_thing") {
loadForm("form2");
goto move_on;
}
if($user_info != "some_yet_another_thing") {
loadForm("form3");
goto move_on;
}
move_on:
?>
Preventing all other CMP instructions at the cascade with a simple JMP, "Assemblying
speaking"