goto statement in PHP

Goto or not to go?

Goto is a very controversial programming statement. When discussing goto, some programmers talk about "spaghetti code" and think of BASIC, others answer with "performance" and think of C.

I'm one of the latter guys. My attitude to coding is, the coder should be able to decide what's the best way (in any means) to achieve the development goals.

Okay, there are only few good reasons to use the goto statement. Actually I can think of only one good reason: leaving a function at its end and place a generic cleanup there ("single point of exit").

Example:

function foo(){
  if( $error1 ){
    echo( 'error condition 1' );
    goto cleanup_and_leave;
  }
 
  if( $error2 ){
    echo( 'error condition 2' );
    goto cleanup_and_leave;
  }
 
  cleanup_and_leave:
  // * (do the usual cleanup here)
 
  return;
}

At the asterisk the code may fulfill tasks of any complexity for purposes like cleanup, preparing return values or whatever has to be done on each return.

The great advantage is we have build a single point of exit. The structure of our code therefore has become much clearer than with nested conditions (if{if{if{if{/*vomit*/}}}}) or several return statements.

Bridge to PHP

PHP is a real spaghetti language by nature. Implicit variable declaration, dynamic typing, lots of hard to handle dynamics. It's hard to understand why the goto statement was just introduced with version 5.3, I would have expected it in version 0.1 beta! Perhaps it's because PHP coders are rather unexperienced and would abuse goto.

So yes, PHP 5.3 and later know the goto statement and accept it like shown in the above code. And to avoid spaghetti-like usage, there is a limitation to goto that's not present in C or BASIC: "It is not allowed to jump into loop or switch statement. Fatal error is issued in such case." (quote of docs at PHP.net)

Well I feel fine with all that! PHP rulez (more and more).

This attitude of being

This attitude of being 'ashamed' for using goto has been frustrating for many years. So I welcome your comment "...the coder should be able to decide...".
goto is a bad habit if a novice uses it when desperate, but is a tool for the experienced coder.

sorry, im a huge newb at this

sorry, im a huge newb at this - i barely understand php but am beingmade to use it at work anyway O_O

what I want to know is can I use the goto statement in the BASIC style, ie to go to another sets of attributes. I am trying to code an attribute set for a product on our online shop, the attributes are Colour, qty and price - i need to link from the single price attributes to the attributes for a pair (variables = price and two sets of colopur choices, ie you can choose two colours for the pair. thanks!

Just see above for an example

Just see above for an example of usage. PHP 5.3 will support that syntax. Versions prior to 5.3 won't.

I wonder what you mean with "link from attribute to attribute". Goto will not (and has never) been used for linking attributes or any kind of data. The goto statement changes program flow in a way that the execution continues somewhere else in the code. This behavior is the same for BASIC, C and PHP (and many other languages I don't know).

Similar constructs are used in ASM (assembler) and thus in machine code (which is directly executable CPU code). So goto ist very much more low level and advanced than many people think.

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is to verify you're a human visitor with at least everage IQ. Please do not use a calculator.
4 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.