Jump to content

Goto / Gosub Equivalent in PHP ?


myrddinwylt

Recommended Posts

Here is a question bugging me for a long time. In many languages, C, Delphi, VB, ASP, even ASM, allow for some internal call which allows you to tell the program to skip code and go to a particular pointer, then continue from there.  What is the equivalent to the following in PHP

 

y = 2
x = 4
startover:
do
    z = y * x
    if z = 396 then goto finalcall
    if y > 200 then exit do
loop
goto codeexit

finalcall:
  print y
  y = y + 1
  goto startover

codeexit:

 

The above code contains a couple of examples where "goto" comes in handy.  Inside the loop, it also contains a condition on which the loop would exit. I understand that particular condition could be included in the loop initialization, however, I am only displaying the logic question in it's simplest forms. In the real world, the logic required to exit the loop would be much more complex, and perhaps entailing many different conditions to exit the loop based on variables that are set or a particular functions result using values passed as designed in the loop code itself. In PHP, I have found no way to exit a loop without having the conditions pre-defined in loop initialization (and the loop will never exit until those conditions are met), nor have i found a method to tell the program ...  goto XYZ pointer, and start running the code from there  (aka...  goto startover is a good example of this).

 

Does anyone have an idea about the equivalent commands and pointers in PHP, or is this too, something left out.  ( 8 million ways to connect to MySQL, and no ways to do this ??? )

Link to comment
https://forums.phpfreaks.com/topic/207364-goto-gosub-equivalent-in-php/
Share on other sites

In PHP, I have found no way to exit a loop without having the conditions pre-defined in loop initialization (and the loop will never exit until those conditions are met)

You can always break out of a loop with the break keyword

http://www.php.net/manual/en/control-structures.break.php

Nice,

 

Didn't think it was that simple :)

 

It appears that the syntax is the same as in most other languages that incorporate these commands.

 

As for using "break;" inside a loop, I have never really thought about that as I thought it was something exclusive to "switch" / "case" scenarios.

 

Thank you (surprisingly, this has been bugging me for several years XD)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.