New answer
Recently, I began work at a new employer which had inherited a legacy [bespoke] PHP partial OO and partial MCV framwork. On some of the pages, the views were performing a good amount of business logic without actually displaying anything; each time, these flat PHP files were included (rather than include_once()
) and so what was happening is that many parts of the site were spaghetti and meat balls type scripting (something which GOTO
is supposed to produce so is considered bad practise).
What was happening was that some logic was firing (because it was included multiple times and in multiple places) when it didn't need to be, and creating error logs because maybe one line or one IF
statement block was required in the whole file of hundreds of lines of PHP.
Creating an end-point and adding in goto END;
was a good interim step to reducing the errors logs and from stopping those bits of script from firing when they didn't need to. For instance:
if(empty($userRequest)){
goto END;
}
## The rest of the script from this point needs to fire until...
if(empty($_REQUEST['user_id'])){
goto END;
}
## etc...
Of course, this is only an interim step to actually reduce the amount of spaghetti. I've also moved some necessary functionality to controller classes where it was practical to do so (and created models where the supposed view files were doing the data modelling).
I could have added in IF...ELSE
statement blocks to achieve the same results, but the amount of logic in the script (and the amount of nested IF
statements etc...) meant that adding in goto END;
actually made the script more readable whilst other refatoring was happening.
Eventually, when I've untangled all of the legacy spaghetti, I'll remove the GOTO
statements and make it fully MVC and OO as it should be.
Old answer
Here is an example of using goto
:
<?php
ten:
print "Hello ";
goto ten;
You are right, it is almost never used in PHP programming because modern programming wisdom says that it leads to "spaghetti code" which is more difficult to debug.
However, try not using non-conditional loops (which is what goto
actually is) in machine-level languages. I remember using non-conditional loops with MIPs assembly a few years back.