Why was GOTO included in PHP 5? [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật