Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable length parameter list fpr funcitions

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi

I am trying to write a function that will take quite a few parameters.
I am currently defaulting them to NULL in the function declaration, but i want to be able to call the function with some but not all the parameters.

I have tried to use func_get_args(), but this is no good as i dont know which parameters were passed, only their values.

Is there a way that i can declare a function like...


Code:
function newStar($title=null,$date=null,$name=null,$message=null,$donation=null, $currency=null, $language=null,$type=null, $x=null, $y=null, $z=null, $friends=array()){

// my function internals

}


What i am trying to do is call it like this...

newStar($title,$name,$message,$friends);

if i do newStar($title, ,$name,$message, , , , , , ,$friends);

than i get errors... 

any suggestions would be great, thanks.
 
I get errors" is kind of vague. Can you be more specific as to what kind of errors it throws back?




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
missing parameter errors...


Anyway, i have re-written the function now as i figured that func_get_args() is useless as you cannot tell what the parameter is that is being passed...you just get a load of values... which is a bit random, and similar to if i passed all the stuff in an array.
 
The only way to know what gets passed is to check for it:

take this example:

Code:
function myfunc($param1=NULL,$param2=NULL, $param3=NULL){

if(isset($param1)){
echo "Param1 was sent with this value:" . $param1;
}

if(isset($param2)){
echo "Param2 was sent with this value:" . $param2;
}

if(isset($param3)){
echo "Param3 was sent with this value:" . $param3;
}

if(!isset($param1) && !isset($param2) && !isset($param3)) {
echo "No parameters where sent";
}

}
$myfunc("something"); will pass the value to the first paramter in the function. in this case param1.

Of course if you intended to pass only say the second paramter, you have to specify that.

$myfun(NULL,"something");

This means the first paramter is NULL, the second has "something" and the third is not passed.

You can only not pass paramaters after you've passed something, or specifcly set it to be NULL. otherwise the param list is taken as it was declared.









----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ah i see, the problem i had was that i was trying to pass empty strings instead of null.

Thanks for your help.

Jez
 
An alternative is to pass your values in an associative array. You can then tag a descriptive name to every parameter you pass.

It can make your function calls a little more complex:

some_function (array('name' => $name, 'dob' => $dob));

but if you routinely call the function with parameters missing, the additional complexity might balance against multiple "NULL, NULL, NULL" values in your function call.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top