Hi

I've noticed that often in php functions there are obligatory arguments so that the function can work and also one or several optional arguments

i was wondering if it's possible to write a custom function that has, say, 2 obligatory args and then if you send more than 2 arg the function does extra tasks

if it is possible i have 2 questions :

what would the syntax for the args be ?

what code would i need to use for detecting if more than 2 args have been sent ?

thanks for your help

    what would the syntax for the args be ?

    It sounds like you want to use default arguments, so it would be something like this:

    function foo($arg1, $arg2, $arg3 = false, $arg4 = null)

    what code would i need to use for detecting if more than 2 args have been sent ?

    Check if the optional arguments have the default values. If they do, then you assume that the caller does not want the extra tasks to be performed.

      that was exactly what i needed

      many thanks

        You can also look at [man]func_num_args[/man]; this only counts arguments supplied by the caller, and not any defaults that may have been used. So to laserlight's

        function foo($arg1, $arg2, $arg3 = false, $arg4 = null) 

        foo(42,17) is supplying two arguments,
        foo(41,17,true) is supplying three, and
        foo(40, 16, false, 'handball', array('not','a','nice','doggy')) is supplying five.

        func_num_args() would return 2, 3, and 5, respectively.

          Write a Reply...