myrddinwylt Posted July 10, 2010 Share Posted July 10, 2010 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 ??? ) Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 10, 2010 Share Posted July 10, 2010 http://php.net/manual/en/control-structures.goto.php Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 10, 2010 Share Posted July 10, 2010 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 Quote Link to comment Share on other sites More sharing options...
myrddinwylt Posted July 10, 2010 Author Share Posted July 10, 2010 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.