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.
exit.