View Single Post
Old 01-20-2008, 10:39 AM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Hi Sarmenhb,

There are a couple of ways to achieve what you want:

The first is to provide your function with a default value in the event that one isn't provided - see Xenon's example. In this case, he has set the default value to NULL, then checks against that in the function.

The second way is to use func_num_args() as you tried:

PHP Code:
function br()
{
    
$numargs func_num_args();
    
    if (
$numargs == 0)
    {
        die(
'You must provide a number!');
    }
    else
    {
        
$num func_get_args(0);    // Gets the first argument
        
echo "You provided $num !";
    }

This method also allows you to accept an unknown number of arguments if your function needs it.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote