Page 1 of 1

goto not working with php!!

Posted: Wed Jul 01, 2009 11:23 pm
by arya6000
Hello

I tried to add a "goto" to my program and I kept getting compile errors, so I wrote the most basic program just with goto to see if it works, and it did not, this is the code

Code: Select all

<?php
 
a:
echo "test";
 
goto a;
 
?>
 
and this is the error I get when I run it in the console
Parse error: syntax error, unexpected ':' in /root/cl/php/m4w/1.php on line 3
I'm running php5 on Debian, any idea why its not compiling?

Regards

Re: goto not working with php!!

Posted: Wed Jul 01, 2009 11:39 pm
by arya6000
McInfo wrote:Run this to make sure you are using PHP >= 5.3.

Code: Select all

<?php
echo phpversion();
?>
Also, your script makes an endless loop, which is why goto's use has been frowned on in programming courses over the years.
I'm using

5.2.6-1+lenny3debian

I'm guessing the only way is to upgrade?

Re: goto not working with php!!

Posted: Thu Jul 02, 2009 2:33 am
by requinix
This begs the question:

Why the hell do you want to use goto?

Re: goto not working with php!!

Posted: Thu Jul 02, 2009 3:24 am
by mattpointblank
Well, if the poster thinks they're "compiling" PHP...

Re: goto not working with php!!

Posted: Thu Jul 02, 2009 4:24 am
by Christopher
tasairis wrote:This begs the question:

Why the hell do you want to use goto?
There are several classes of problems where goto results in smaller, clearer and cleaner code. Any kind of state machine, but parsers are the classic example. Anywhere where using standard structured programming produces deep nested ifs. Also problems that have multiple exit condition blocks. Certainly these kinds of problems are infrequent, but they just as certainly exist.

And for the record, the new PHP goto is specifically limited to reduce some of the original complaints about gotos. For example, you cannot jump into a loop, only out. Think of them more as "break to label."