0

I'm looking for a smarter/shorter way of doing the following:

function main() {
   if (!otherfunction()) {
      return; // exit main function
   }
   doMoreStuff();
   ...
}

Instead, I want the exit in the "main" function to be determined by a function called within the "main" function, like this:

function main() {
   otherfunction(); // exit main function depending on what's going on in otherfunction() 
   doMoreStuff();
   ...
}

Oh-dear-oh-dear-oh-dear-oh-dear - pardon my $#x! here. I hope, you understand what I mean. If so, feel free to propose a better way to describe it.

1
  • Not possible except to end the execution of the entire PHP script as shown below using exit. Commented Oct 30, 2019 at 18:47

2 Answers 2

1

You can't return out of a function from the inner one. You have to capture some of the control flow in some manner within main(). You have a few options for doing this.

First, you could use an if statement as you've outlined above:

function main() {
    if(!otherFunction()) {
        return;
    }

    doMoreStuff();
}

This approach is perfectly acceptable and is very readable. I use this simple approach frequently in my own production code.

Second, you could use exceptions and try/catch control:

function main() {
    try {
        otherFunction(); // May throw an exception if you want to exit early.
        doMoreStuff();
    } catch(Exception $e) {
        // Do nothing; just catch the exception and allow the rest of main() to finish.
    }
}
// Alternatively
function main() {
    otherFunction();
    doMoreStuff();
}

try {
    main();
} catch(Exception $e) {
    // Do nothing; silently fail.
}

This isn't particularly recommended, but is a valid option available to you.

Lastly, you can use short-circuit evaluation of conditional expressions:

function main() {
    otherFunction() && doMoreStuff(); // if otherFunction() returns a non-"truthy" value, then doMoreStuff() won't execute.
}

This is also a perfectly valid approach that you'll frequently see in shell scripting for one-liners.

My personal recommendation is to keep the simple early-return pattern using the if statement as your intentions are clear and it's easy to add additional functionality. The other options will work, but your intentions won't be as clear and it may become more difficult to add functionality to your program. Ultimately the decision is yours to make, however.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use exit on otherFunction() based on an if statement, if you are willing to end the execution of the entire PHP script, like so:

function doMoreStuff()
{
    echo 'Test';
}

function otherfunction()
{
    if(some condition) {
        exit;
    }
}

function main() {
    otherfunction();
    doMoreStuff();
}

In that case, if the condition returns true it will terminate the current script, or else it will echo 'Test', which is the output of doMoreStuff() function.

Is this what you want to achieve?

1 Comment

not quite - I need to keep the script running after calling the main function, so I can't use exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.