This isn't really a question I just noticed on the php manual under control structures that they are going to be using goto. I don't know how old any one is in this forum, but when I saw that it brought me back to the days of when a cd-burner was a tape drive and if you wanted a floppy drive you were talking some decent cash, Internet was BBS, if you could afford the telephone bill, I couldn't even afford the modem. On my tandy trs-80 programming was pretty BASIC. I was only 9 so programming was very basic, well still is. Buying games on tape was not unheard of. Not much in the php manual about this but hopefully it will not process the whole script and just process what is needed. Now I feel old. :icon_cheesygrin:

Recommended Answers

All 15 Replies

A goto control structure? That is taking us back to the dim dark past... :P

In all seriousness, if php was heading toward object oriented development, I can't think why a goto would be needed. Is it going to be like the old days, ie goto line 2376? I think that would be particularly dangerous for a number of reasons. Or will it be used in a different way?

goto
The goto operator can be used to jump to other instruction in the program. The target place is specified by the label and a colon and goto is followed by that label.

Example #1 goto example
<?php
goto a;
echo 'Foo';
a:
echo 'Bar';
?>
The above example will output:
Bar

where goto goes to a label would be useful for conditional branching in a switch statement, where the code blocks are large enough that

switch (test) {
case foo:
  { /* 1000 lines of code */ }
break
case bar:
  { /* 1000 lines of code */ }
break
case cant: 
 { /* 1000 lines of code */ }
break
case find: 
 { /* 1000 lines of code */ }
break
case the: 
 { /* 1000 lines of code */ }
break
case nexxt: 
 { /* 1000 lines of code */ }
break
case casse:: 
 { /* 1000 lines of code */ }
break
}

becomes a more closely nested switch stament without 1000 lines of code between each case,
just

case foo:  { goto foocase; }
break
case bar:  { goto barcase; }
break
//rest of switch statement
foocase: /* 1000 lines of code */
barcase: /* 1000 lines of code */
cant: //etc
find: //etc
the: //etc
nexxt: //etc
casse: //etc

The problem with that almostbob is that if you have a switch-case with 1000 lines of code you're a horrible programmer to which the "goto" is only the stamp on your forehead to label(no pun intended) you as such.

view source of 'professionally' designed scripts
bloat's the least of the problems
bad practice is about the only reason for a retrograde step.
I noted a use, not my use, personal attacks are unnecessary

view source of 'professionally' designed scripts
bloat's the least of the problems
bad practice is about the only reason for a retrograde step.
I noted a use, not my use, personal attacks are unnecessary

I wasn't attacking you, I was just saying that if someone uses a switch-case like that then a goto is the least of their problems. It was a general statement, not an attack at one specific person.

There are cases where GOTO enables clearer code.
Consider this example - first without GOTO:

if ( condition_a ) {
  $out['x'] = func_a();
  if ( condition_b ) {
     $out['y'] = func_b();
     list_print ( $out );
     exit;
} else {
  $out['x'] = func_c();
}
$out['z'] = func_d();
list_print ( $out );

So, we have to call 'list_print' twice and we 'exit' from inside a nested structure - not very elegant.
With GOTO the same logic would look like this:

if ( condition_a ) {
  $out['x'] = func_a();
  if ( condition_b ) {
     $out['y'] = func_b();
     GOTO 'list';
} else {
  $out['x'] = func_c();
}
$out['z'] = func_d();
label 'list';
list_print ( $out );

Looks clearer and simpler to me: 'list_print' happens at the end and that's where it stands in the code.
Changes like 'list_prettier($out)' will be less error-prone. And, if you didn't read the whole code, the 'label' is a hint on the fact that 'list' is called from other places.
A similar structure could be achieved with a function, but then the code gets bigger, the function code is somewhere else, plus we have to consider variable scope; functions also cost performance.

Yes, ugly things can be done with GOTO, but ugly things can be done with a steering wheel as well. Still no one tried to sell me a tram instead of a car. BTW, I love motorbikes.

here's something ugly for ya.

<?php
goto dsiembab;
another:
echo "another ";
stupid:
$subject = "abcdef";
$pattern = '/^def/';
$holla = new Zend_Simple_Awesome_Buzzword_Compliant_OOP_Wrap_Native_Function;
$holla->preg_match($pattern, $subject, $matches, REG_OFFSET_CAPTURE, 3);
naive:
echo "naive ";
example:
echo "example ";
by:
echo "by Dsiembab <br />"
dsiembab:
echo "yeah that's right I code like chef boyRD";

If you can make yourself laugh that's half the battle. :)

I don't get the joke.
What are you trying to say?

yeah that's right I code like chef boyRD

Goto statements lead to spaghetti code. Very quickly.

This potential pitfall is well known for decades, so if one wants to avoid it safely - go for the tram.

I just checked the official documentation and although it can be useful to jump out of loops to before the loop had began, I found the effects of using it incorrectly could cause a fatal server error (example: crashing apachie) Below is an example of escaping the loop.

<?php
$gotostart=0;
start:
$gotostart+=1;

//some code
echo "This is before the loop<br>";

$statement=0;
while($statement<5)  {
    //some code
    echo "This is in the loop before repeat<br>";
    if ($gotostart<3) {
    goto start;
    }
    echo "This is in the loop after repeat<br>";
    $statement+=1;
}
echo "Page loaded.";
?>

So with the above example, it will process the loop once without repeating the loop then will goto the start. After that the script will go through the loop again but won't repeat the loop so the loop acts like if there is no loop at all. Then when it reaches the goto again and will process the script again but on this third time it will repeat within the loop without the goto 5 times. Then will continue with the rest of the script. So the only alternative to the not using the goto in the script above is having a loop starting at the start: and ending at the goto. So in situations like this it can speed up the code (like it would have in my search engine bot) but if you don't use the goto element in the right places then it can slow your code to pieces.

Now about I mentioned earlier how it can crash the apachie service. An example is the following code:

<?php
$gotostart=0;
start:

//some code
echo "This is before the loop<br>";

$statement=0;
while($statement<5)  {
    //some code
    echo "This is in the loop before repeat<br>";
    goto start;

    echo "This is in the loop after repeat<br>";
    $statement+=1;
}
echo "Page loaded.";
?>

The above example is a classic just like the infinit while loop. Another one I learnt is if you have a script that has the following:

<?
file_get_contents('http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']);
?>

Amazing how php doesn't know if it is using too much cpu.

But you don't need a Goto to code an infinite loop, it's much easier:

while ( always_true() ) {
  some_func();
}

"Amazing how php doesn't know if it is using too much cpu." - So you're asking for a separate performance-analysis-daemon that is started with every skript, right?

...
"Amazing how php doesn't know if it is using too much cpu." - So you're asking for a separate performance-analysis-daemon that is started with every skript, right?

Well I am just saying that the php libraries should take longer to execute and there for taking less cpu instead of having to use the sleep() function for extreamly large scripts. Or maybe even have an ini priority number for how fast/cpu hungry the script should be.

Sleep() for large scripts .. => Another thread please.

For this thread I hope a useful application for GOTO has become clear.

For this thread I hope a useful application for GOTO has become clear.

Yes, one use came to mind immediately. It is great for making messy code! The knowledge that it is a poor practice has been know widespread for decades. So why is your case is an exception?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.