One can write bad code in any language. Having goto
in the language doesn't mean the language is bad.
Consider existing structures in various languages - break, continue
, next
, last, throw
. Even switch
These are all forms of a computed go to that go from one level of scope to the same or higher level of scope.
The problem with goto is when it enters a deeper level of scope or jumps stack frames.
One will note the Example 3 in the linked document from the question:
<?php
goto loop;
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
loop:
}
}
echo "$i = $i";
?>
This will not work because the goto
is attempting to jump into a deeper level of scope.
There is nothing wrong with various forms of restricted goto, be the keyword named goto or some other branching name. The alternatives (ugly conditionals in for loops) may often be worse. As it is, using goto simplfies the
Personally, I don't like the example given as example #1
<?php
goto a;
echo 'Foo';
a:
echo 'Bar';
?>
However, this doesn't cause problems with the language other than the necessity to apply some form corporal learning experience to the developer who intentionally writes code like this.