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 ;) - programming help at Thu, 01 Apr 2010 11:35:03 +0200 +1 Les is right - sherry william at Sat, 10 Apr 2010 10:58:14 +0200 Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document. As a general-purpose programming language, PHP code is processed by an interpreter application in command-line mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as standalone interpreter on most operating systems and computing platforms. - PHP Project at Tue, 20 Apr 2010 13:36:24 +0200 I'm currently using the object-oriented possibilities of php, but that's only because the better things like "goto" aren't currently available. It would be a very nice addition to the language indeed.=) - Ahmad Al Jayousi at Tue, 23 Nov 2010 14:34:53 +0100 Ahmad Al Jayousi Philadelphia University Jordan http://www.philadelphia.edu.jo E-mail: aaljayousi@philadelphia.edu.jo - Jaime at Wed, 04 May 2011 17:09:12 +0200 Quiero ve en donde sale esto!!! - ThoughtfulBlogger.com at Fri, 30 Mar 2012 19:39:45 +0200 Wir freuen uns sehr über den Schritt von Galileo Computing, unser Buch nun ins Netz zu stellen. Kommentare und Feedback können gerne an dieser Stelle hinterlassen werden. - Fictitious Blog at Thu, 05 Apr 2012 13:07:39 +0200 We need to follow certain rules and regulations to avoid these kind of situations, thanks.