I discovered some time ago that the GOTO control keyword was introduced in PHP 5.3.0.


Why did it happen?

What are the language design goals behind this?

Did the PHP developers community asked for it?

  • 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

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)

  • 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 2:48

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;

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.

  • 2
    goto cleanup? Seriously? Why not just implement a real try-finally construct? (Does PHP have one?) – Mason Wheeler Dec 19 '12 at 22:13
  • 3
    @MasonWheeler PHP got finally fairly recently, and after it got goto. – yannis Dec 19 '12 at 22:36
  • 3
    Well, there are people who don't like exceptions but goto. It's a free world ;-) – johannes Dec 19 '12 at 22:53
  • 7
    And btw. being the Release Master of PHP 5.3 I take the full responsibility for this decision :-) – johannes Dec 19 '12 at 22:55

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:

goto loop;
for($i=0,$j=50; $i<100; $i++) {
  while($j--) {
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

goto a;
echo 'Foo';

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.

  • 4
    +1: especially "having goto in the language doesn't mean the language is bad" – Deco Dec 19 '12 at 23:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.