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
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.