8

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?

Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
  • 10
    I don't see a problem with goto really. I really don't. Yet people seem to loathe it, even though it's almost *never* used even in cases in which it would simplify the code. Yes, it has good uses, regardless of if you are used to reading `goto` in the source or not. Whether they're often seen in idiomatic code or not is another thing. Goto causes no harm however. – zxcdw Dec 19 '12 at 20:53
  • 7
    Finite state machines are a tad easier to code when you have `goto`. – yannis Dec 19 '12 at 21:07
  • Related: [Is it ever worthwhile using goto?](http://programmers.stackexchange.com/questions/566/is-it-ever-worthwhile-using-goto) & [Do we still have a case against the goto statement?](http://programmers.stackexchange.com/questions/125715/do-we-still-have-a-case-against-the-goto-statement) – yannis Dec 20 '12 at 01:15

3 Answers3

22

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)

Sam Onela
  • 117
  • 9
grahamparks
  • 2,099
  • 1
  • 15
  • 14
  • 19
    So it was done for a project he no longer works on... Sweet. – Robert Harvey Dec 19 '12 at 21:12
  • Aye captain! `every construct from every programming language ever invented`! ...to infinity, and beyond! (and to think that's what they used to say of Perl 5... I'm so proud of this evergrowing little blob of nonsense called PHP) – ZJR Dec 21 '12 at 02:48
13

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.

yannis
  • 39,547
  • 40
  • 183
  • 216
johannes
  • 3,601
  • 2
  • 26
  • 32
5

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.