View Single Post
Old 06-16-2008, 12:09 AM   #7 (permalink)
delayedinsanity
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Technically you could have an unlimited amount of optional arguments. The first way, and probably the least aesthetically pleasing would be to define a single argument and check if it's an array or not;

PHP Code:
function snuffleluffagus ($mArgs) {

    if (
is_array($mArgs)) {
        
$intNumArgs count($mArgs);
        
// ..
    
} else {
        
// ..
    
}


..and the second would use PHP's built in functionality, with func_num_args() and func_get_args().

PHP Code:
// example borrowed directly from PHP.net

function foo () {

    
$numargs func_num_args();
    echo 
"Number of arguments: $numargs<br />\n";
    if (
$numargs >= 2) {
        echo 
"Second argument is: " func_get_arg(1) . "<br />\n";
    }
    
$arg_list func_get_args();
    for (
$i 0$i $numargs$i++) {
        echo 
"Argument $i is: " $arg_list[$i] . "<br />\n";
    }


-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Yoosha (06-17-2008)