soma56 Posted June 19, 2010 Share Posted June 19, 2010 I have a situation where if certain criteria isn't met I need the function to be 'recalled' or otherwise start from the beginning. For example: function beach(){ echo $do_something; if ($do_something != $fun) { echo "HOW DO I stop and START THE FUNCTION AGAIN FROM BEGINNING?" } else { exit; } beach(); For some reason I think a loop is in order here? Quote Link to comment https://forums.phpfreaks.com/topic/205268-how-to-recall-same-function-within-that-function/ Share on other sites More sharing options...
jskywalker Posted June 19, 2010 Share Posted June 19, 2010 function beach(){ while ($do_someting != $fun) { echo $do_something; } } Quote Link to comment https://forums.phpfreaks.com/topic/205268-how-to-recall-same-function-within-that-function/#findComment-1074419 Share on other sites More sharing options...
Tiruak Posted June 19, 2010 Share Posted June 19, 2010 Well, it probably has a more efficient way to do that, but one thing you could do is create a second function that all it does is to call the function beach(). Something like: function call_beach() { beach(); } Then, if your criteria isn't met, you can just call the funcion call_beach() Quote Link to comment https://forums.phpfreaks.com/topic/205268-how-to-recall-same-function-within-that-function/#findComment-1074420 Share on other sites More sharing options...
Alex Posted June 19, 2010 Share Posted June 19, 2010 Well, it probably has a more efficient way to do that, but one thing you could do is create a second function that all it does is to call the function beach(). Something like: function call_beach() { beach(); } Then, if your criteria isn't met, you can just call the funcion call_beach() What exactly would the purpose of that be? Calling call_beach() would be exactly the same thing as calling beach(). If you need to call the function from inside the function go ahead, what's stopping you? Just make sure you don't create an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/205268-how-to-recall-same-function-within-that-function/#findComment-1074423 Share on other sites More sharing options...
KevinM1 Posted June 19, 2010 Share Posted June 19, 2010 Recursion may be the answer, depending on how $do_something is supposed to be set. function beach() { echo $do_something; $do_something = $some_other_thing; // <-- this is the key part... $do_something needs to eventually become 'fun', else you'll have an infinite loop if ($do_something !== 'fun') { beach(); } else { exit; } } Quote Link to comment https://forums.phpfreaks.com/topic/205268-how-to-recall-same-function-within-that-function/#findComment-1074424 Share on other sites More sharing options...
soma56 Posted June 19, 2010 Author Share Posted June 19, 2010 The """ if != """ is the easiest for me to understand and worked. I appreciate everyone's insight as always Quote Link to comment https://forums.phpfreaks.com/topic/205268-how-to-recall-same-function-within-that-function/#findComment-1074470 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.