goto not working with php!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arya6000
Forum Newbie
Posts: 15
Joined: Fri Apr 07, 2006 3:32 pm

goto not working with php!!

Post 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
arya6000
Forum Newbie
Posts: 15
Joined: Fri Apr 07, 2006 3:32 pm

Re: goto not working with php!!

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: goto not working with php!!

Post by requinix »

This begs the question:

Why the hell do you want to use goto?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: goto not working with php!!

Post by mattpointblank »

Well, if the poster thinks they're "compiling" PHP...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: goto not working with php!!

Post 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."
(#10850)
Post Reply