Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as not a real question by mattnz, Dynamic, Walter, ChrisF Dec 20 '12 at 12:25

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

8  
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
6  
Finite state machines are a tad easier to code when you have goto. –  Yannis Rizos Dec 19 '12 at 21:07
    
add comment

3 Answers 3

up vote 15 down vote accepted

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 defence, 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)

share|improve this answer
15  
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
add comment

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.

share|improve this answer
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 Rizos Dec 19 '12 at 22:36
2  
Well, there are people who don't like exceptions but goto. It's a free world ;-) –  johannes Dec 19 '12 at 22:53
5  
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
add comment

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.

share|improve this answer
4  
+1: especially "having goto in the language doesn't mean the language is bad" –  Deco Dec 19 '12 at 23:40
add comment

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