I discovered some time ago that the GOTO control keyword was introduced in PHP 5.3.0.
http://php.net/manual/en/control-structures.goto.php
Why did it happen?
What are the language design goals behind this?
Did the PHP developers community asked for it?
3
The person responsible gives the reason for adding it in this blog post.
[T]he project I was working on (and may pick back up someday) was a
workflow based site builder […] for making dynamic websites without
knowing a programming language. You build up all the meta-information
about how your site should run, then the site-builder “compiles” that
into PHP code that’ll run on the actual server.It can be done without GOTO, but the resulting logic for the
particularly complex bits of business logic become much simpler with
it.
In PHP’s defense, it only lets you jump to labels within a limited scope, which limits the harm that can be done.
(You might also argue that given the way PHP has apparently absorbed every construct from every programming language ever invented, the addition of goto
was simply inevitable)
2
On a lower level PHP had goto
since PHP 4. With PHP 4 the interpreter was redone and the Zend Engine was introduced, which includes a compiler that compiles down to low-level “opcodes” which are handed over to an executor.
On that level a jump operation is used for all if
, switch
or any loop statements. With PHP 5.3 it was decided to export that under the name of goto
after a long discussion.
The main reasons were:
- It can be done without any cost (we already had it internally anyway)
- It can help when writing code generators
- Sometimes it can help to clean up with error handling code.
The first two items might not need explanation, and the third part is about situations like this:
function do() {
$lock = $acquire_lock...)
if (!do_something()) {
goto cleanup
}
if (!do_more()) {
goto cleanup;
}
cleanup:
free_lock($lock);
Of course you have few such situations in PHP, as PHP has a reference counted garbage collection system which will predictably free such variables and associated resources. Nonetheless such situations exist where developers either duplicated the clean up code multiple times or abuse ddo { } while (0);
with break
as goto
emulation.
To prevent misuse the implementation was limited in a way that you can not jump into loops. So “abuse” in the way of the famous Duff’s Device won’t be possible.
In the end PHP is being developed to solve problems, it is not fully protecting you from shooting in your own foot, you can abuse many features in way worse ways than this limited goto
. So, the conclusion was there are edge cases where it is useful and can solve problems.
4
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.
1