schlitt.info - php, photography and private stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :Author: Tobias Schlitt :Date: Wed, 01 Apr 2009 23:25:28 +0200 :Revision: 2 :Copyright: CC by-nc-sa ============= GOSUB for PHP ============= :Abstract: Since version 5.3 PHP supports the GOTO statement. While this statement is useful to solve daily programming tasks like implementing finite state machines, it has some serious drawbacks against other programming languages that support the structured programming paradigm. Therefore the PHP core developers decided with one voice to enhance the language by a GOSUB statement. :Description: Since version 5.3 PHP supports the GOTO statement. While this statement is useful to solve daily programming tasks like implementing finite state machines, it has some serious drawbacks against other programming languages that support the structured programming paradigm. Therefore the PHP core developers decided with one voice to enhance the language by a GOSUB statement. **Attention: This post is an April fool hoax! There are no plans by anybody for GOSUB or similar in PHP!** Since version 5.3 the PHP programming language finally supports the GOTO__ operator. GOTO is a `standard construct`__ in high-level programming languages and has a long history in paradigm of `structured programming`__. The GOTO statement allows to solve daily programming tasks, like implementing `finite state machines`__, very easily and comfortably. GOTO constructs raise the code quality of such implementations drastically. .. __: http://php.net/goto .. __: http://en.wikipedia.org/wiki/Goto .. __: http://en.wikipedia.org/wiki/Structured_programming .. __: http://en.wikipedia.org/wiki/Finite_state_machine PHP supports so-called labeled GOTO, in lack of forced source code line numbering. An example of how GOTO works in PHP is shown below: .. include:: data/0705_goto_example.php :literal: *main* and *subroutine* are labels in this simple PHP script. Inside the if statement, the script performs a jump to the label *subroutine*, if the *$counter* is not larger than 5, yet. This jump results in the execution flow to jump to line 20, where the label is declared. After printing out the text, another jump is performed, back to the *main* label. As can be seen in this example, a major drawback in the PHP implementation of GOTO is that the more advanced version GOSUB__ is not supported, yet. The GOSUB statement allows you to return from a sub-routine right to the next statement after the jump. It allows you to simplify the code above drastically as can be seen below: .. include:: data/0705_gosub_example.php :literal: .. __: http://en.wikipedia.org/wiki/GOSUB In this script, GOSUB is used instead of GOTO, making the *main* label unnecessary. The return statement after the *subroutine* label makes the execution flow jump right back to the next statement after the GOSUB call. Beside cleaning up the shown code, GOSUB has the drastic benefit, that it makes labeled sub-routines re-usable: The jump back is not bound to a fixed label, but returns to the place where the routine was entered. To fix this drastic drawback against more advanced GOTO implementations, like e.g. in Basic, the PHP core developers decided to delay the release of PHP 5.3 once more. The current release candidate state of PHP 5.3 is canceled and the development team is now working on another alpha version. Due to the complex changes necessary in the parser, the stable version of PHP 5.3 can not be expected before January 2010. Rasmus Lerdorf, the inventor of PHP, stated, that he is highly pleased to see this change coming in so straightforward. He is sure, that this is an important addition to the PHP programming language and supports the newly decided delay of version 5.3 to fix the misery. Rasmus said: "It is unacceptable that PHP is struck off by other high-level languages like Basic__". .. __: http://www.qbasicnews.com/qboho/qckadvr.gosubr.shtml .. Local Variables: mode: rst fill-column: 79 End: vim: et syn=rst tw=79 Trackbacks ========== Comments ======== - fullo at Wed, 01 Apr 2009 10:16:46 +0200 you are absolutely right! It's a shame to not have a complete goto/gosub implementation! ;) - berry__ at Wed, 01 Apr 2009 10:30:44 +0200 I'm currently using the object-oriented possibilities of PHP, but that's only because the better things like "goto" and "gosub" aren't currently availiable. It would be a very nice addition to the language indeed. ;) - Wolfgang Stengel at Wed, 01 Apr 2009 16:34:16 +0200 Why would you need gosub when there are functions? - Les at Wed, 01 Apr 2009 16:57:37 +0200 This must be some sort of April fools day joke, surely? Please, let it be. It was a -beep- mistake having GOTO in PHP but now GOSUB? May I suggest to the individual(s) responsible that they GOTO -beep- ? ;) I'm not really amused and if this is the route that PHP is taking, then it's a very bleak, black day for PHP developers the world over. - mor10am at Wed, 01 Apr 2009 19:21:26 +0200 I would be great if one day all my C64 Basic programs would run in PHP! - Patrick at Wed, 01 Apr 2009 21:07:22 +0200 I totally agree with Les. It's the exact opposite to all I have learned at the University. If you only mentioned the word GOTO you just about failed the course instantly.... - Markus at Wed, 01 Apr 2009 22:26:06 +0200 @mor10am: Full ACK!! I hereby propose implementing PEEK and POKE next. After that is done, we can replace require and include with LOAD "*",8,1 :-P - Engelbert at Fri, 02 Oct 2009 01:33:43 +0200 It requires your server to have the 16k RAM pack - Toby at Fri, 02 Oct 2009 07:48:35 +0200 @Engelbert: LOL ;) - optimizar seo at Thu, 16 May 2013 13:19:17 +0200 When you buy a gift for a boy, take his age, class grade and maturity level into consideration. The closer he comes to his teen years, the more he relies on his peers' opinions as to what is "cool". Electronics and science related gifts are possibilities, as are sporting goods. - resturant reviews at Thu, 16 May 2013 13:19:39 +0200 One of the most inexpensive and sturdy materials for constructing such a bench is 2-by-4 boards. - furniture concepts at Thu, 16 May 2013 13:19:54 +0200 Invest in your future as a fencing contractor and join the AFA. The cost to join AFA is an annual fee of $500, including an additional fee of $100 to $200 depending on the local AFA chapter you decide to join - home furniture ideas at Thu, 16 May 2013 13:20:11 +0200 Painting furniture involves sanding, priming and painting. Having the patience to apply a second coat of paint, will reward you with a better-looking, longer-lasting finish. Painting wood furniture is very easy to do yourself, just follow the steps below