note 123478 added to control-structures.goto

From: Date: Sat, 29 Dec 2018 16:45:54 +0000
Subject: note 123478 added to control-structures.goto
Groups: php.notes 
Request: Send a blank email to php-notes+get-209971@lists.php.net to get a copy of this message
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" ---- Server IP: 208.43.231.9 Probable Submitter: 2a02:cb43:e::301 (proxied: 2.81.66.133) ---- Manual Page -- http://php.net/manual/en/control-structures.goto.php Edit -- https://master.php.net/note/edit/123478 Del: integrated -- https://master.php.net/note/delete/123478/integrated Del: useless -- https://master.php.net/note/delete/123478/useless Del: bad code -- https://master.php.net/note/delete/123478/bad+code Del: spam -- https://master.php.net/note/delete/123478/spam Del: non-english -- https://master.php.net/note/delete/123478/non-english Del: in docs -- https://master.php.net/note/delete/123478/in+docs Del: other reasons-- https://master.php.net/note/delete/123478 Reject -- https://master.php.net/note/reject/123478 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209971) next »