Jump to content

How to recall same function within that function


soma56

Recommended Posts

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?

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()

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.

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; }
}

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.