Anna K.
Anna K.

Reputation: 1995

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 :(

Upvotes: 2

Views: 2130

Answers (3)

Daryl Gill
Daryl Gill

Reputation: 5524

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

Upvotes: 2

Jhonathan H.
Jhonathan H.

Reputation: 2713

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

Upvotes: 0

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

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.

Upvotes: 3

Related Questions