tags:

views:

54

answers:

3

Assume I have a method/function with the following signature:

foo($bar = 0)

Inside foo, how do I tell if $bar was set or not? isset will alway return a TRUE since $bar is assigned 0 in the event nothing is passed to foo.

Checking for 0 is not an option. I need to know the difference between the parameter explicitly being set to 0 or defaulting to 0.

+2  A: 

Simply use func_num_args() to specifically check for how many arguments were passed in.

<?php
function foo($bar = 0)
{
    echo "\nNumber of arguments: " . func_num_args();
}

  // Outputs "Number of arguments: 1"
foo(0);

  // Outputs "Number of arguments: 0"
foo();
?>    

Live example

Peter Ajtai
A: 

Always use NULL for defaults so you will know if your script set it. It is impossible for user input to set a value to NULL. User input ($_GET/$_POST) can only set FALSE values like 0 or ''

function foo($bar = NULL)
{
    if(isset($bar))

Never check with lose comparisons. For example, the following all equal each other:

0 == FALSE == '' == NULL

Instead you should use strict comparisons

0 === NULL
0 !== NULL
Xeoncross
This is inappropriately categorical. There are many cases where you'd want to specify a non-null default value. Good defaults save time.
banzaimonkey
True, but not according to the OP question.
Xeoncross
NULL as default is not a good thing. You should use good default values.
StackOverflowNewbie
"It is impossible for user input to set a value to NULL." - This is not true. If I type `foo(null)` then I am calling the function with the argument set to null. You are thinking of HTTP requests; the question is about default arguments to functions.
Hammerite
"User input" is data that comes from the user agent. And less you also mistake "user agent" - that is most commonly a web browser. It is **impossible for a user-agent to submit NULL data**. So, If you want a FALSE value to test for empty user data you should use NULL. Then if the value is NULL you know that they did not pass a value in. Obviously, NULL doesn't work for everything so you should use proper defaults as needed.
Xeoncross
+6  A: 

You can use func_get_args. Example:

function foo($optional=null) {
    if (count(func_get_args()) > 0)
        echo "optional given\n";
    else
        echo "optional not given\n";
}

foo(); //optional not given
foo(null); //optional given

Note that the convention used for internal PHP functions is to always give optional arguments a default value and to have them have the same behavior when both argument is not given and its default value is explicitly given. If you ever find otherwise, file a bug report. This let's you do stuff like this without ifs:

function strpos_wrap($haystack, $needle, $offset = 0) {
    return strpos($haystack, $needle, $offset);
}

This convention is more enforced is userland, as the difficulty that led you to this question has shown you. If the convention doesn't suit your needs, at least reconsider your approach. The purpose of func_num_args/func_get_args is mainly to allow variable argument functions.

Artefacto