2

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 :(
6
  • 2
    Functions return null by default, so no, not really. Commented Apr 16, 2013 at 1:50
  • 1
    Out of interest when would you need this? Commented Apr 16, 2013 at 1:55
  • A plugin system. I want to find out if the hooks represent filters or events (events should not return anything). I don't want to create separate functions for them :P Commented Apr 16, 2013 at 2:11
  • I would be more explicit in the registration of such functions, i.e. addEventHook() or addFilter(). Commented Apr 16, 2013 at 2:12
  • 1
    Well, that's your prerogative; it would avoid the "shooting oneself in the foot" problem though; what if a function wants to be both? :) Commented Apr 16, 2013 at 2:14

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

2 Comments

is there a justification for voting down?
I do not see a reason for a downvote for this answer.. It's logical, the only reason I guess is because you have included an echo which does not return nothing, just gives output; so the function will be returning a null even know output is given

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.