I created a conditional statement in PHP lately. I've been bothered if I can put the whole block of a function inside the parenthesis or the expression in IF condition. For example:
if( ! function($some_vars) {
//Execute logic of the function here
return false;
}) {
// Execute if the function above returns false;
}
PS: The code above does not return any error but does not execute the logic inside the function either. My question is: Are the code above possible and if this is possible, is it safe/recommended?
Thanks!
You can use call_user_func to execute such function right now:
if(!call_user_func(function($some_vars) {
//Execute logic of the function here
var_dump($some_vars);
return false;
},"a variable called some_vars")) {
var_dump("yes");
}
But as it's not recommended because of readability. So the following code is more readable:
function checkSomeVar($some_vars) {
//Execute logic of the function here
var_dump($some_vars);
return false;
}
if(!checkSomeVar("a variable called some_vars")) {
var_dump("yes");
}