Passing unknown number of variables in php

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
I want to call a function which in turn calls other functions depending on the variables i pass to it. It may be one variable or two. However, if i only pass one i get the following error...

Missing argument 2 for dis_rightcol()

Is there a way to avoid this?

Thanks
 
When declaring the function, do this to set a default value for variables - this will make passing them optional:

Code:
<?php

function fooBar($time, $start = "0", $end = "12") {
...
}

?>
Then if you call it as follows, $start will have the value '0' and $end will have the value '12':

Code:
$time = "some sort of time value, I hope";

$var = fooBar($time);
Result: no errors :)

So, what you need to do within your function is check the value of the optional values :)
 
Yep, it's not too tricky either,
Code:
function toast(){ //note no args shown in signature
     $args = func_get_args();
     (func_num_args() == 1) ? other_function(args[0]) : 2ndFunction(args[0], args[1]) ;
}

hope that helps?
Colin.
 
Last edited:
Back
Top Bottom