简体   繁体   中英

How to determine if function doesn't return anything?

Is there a way to do this in PHP, using Reflection or something?

function a{
  return null;
}

function b{

}


$a = a(); // null
$b = b(); // null :(

If you do not explicitly return something then functions will return null by default. That is just how functions work in PHP, and there is no way of finding out if the function has a return value.

This should not be a problem, though. If a function returns null it usually means that nothing was done, or that nothing was found, etc.

As you are defining your own functions, you should know yourself if they are returning anything or not.

By in any case. A function returns null by default unless you have overridden the return

function A (){   
}

function B(){
 return 'Test';
}

function C(){
return;
}

function CheckValidation ($Var){
    if (is_null($Var)){
        return 'Sample Is Null';
    }else{
        return 'Sample Is Not Null and returned a value!';
    }
}

echo CheckValidation(A()); // Will output: 'Sample Is Null'
echo CheckValidation(B()); // Will output: 'Sample Is Not Null and has returned a value!
echo CheckValidation(C()); // Will output: 'Sample Is Null'

The function I have provided is the best you are going to get, due to the fact a function returns null by default if there is no return that is..

mind not to return it null because it will definitely display no results.

instead try to add an echo statement inside for checking or return it with a value but still in either case you still have to use echo to output results.....

Also refer to PHP.NET on proper way how to create a USER DEFINE FUNCTION

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM