Reputation: 12193
If I put a function into an if statement is this an effective way to 'declare only if not yet declared'? I read on another thread that functions nested inside others worked the same as non-nested (not sure this applies here). Will my function always execute or only when if is true?
if(!function_exists('myFunction')){
//Add myFunction just in case it wasn't declared previously
function myFunction() {
// do something
}
myFunction();
}
else {
myFunction();
}
Alternatively, is there a better way to do this, being that the function may or may not be previously declared in a parent file?
Upvotes: 0
Views: 71
Reputation: 16369
place function myFunction(){} outside of the if statement if you plan to call it elsewhere (like in your else statement)
Upvotes: 1