counting the number if params passed to function
Posted by John_Moylan (jfp), 9 June 2003Hello All
In perl I have @_ which I can use to count the number of params fed to a function, is there any way i can count the number of params fed to a function in PHP.
Posted by admin (Graham Ellis), 10 June 2003PHP uses a rather different system to Perl, so there's no direct comparison to @_.
You can declare a function that has one or more optional parameters, for example
function dressing($text,$colour = "fushia",$size =
which can take one, two, or three parameters. You can't actually tell within the function how many parameters were given, but if you're looking to find out how many parameters there were so that you can set the rest to a default (probbaly the most common reason to want to knoe), it's already done for you.
Other circumstances do arise where you want to pass a variable and non-specific number of parameters to a function, and the best way to do this is within an array:
and then your code for the function could read:
N.B. - code only manually syntax checked ...
Posted by John_Moylan (jfp), 10 June 2003This was something I had in my head really, I was thinking of counting the params for method overloading, along the lines of a php class that would call a different constructor based on the number of params fed in.
Posted by waygood (waygood), 30 June 2003Actually you can overload functions and count the number of parameters passed to it without using arrays, as you requested.
echo "Number of arguments is : " . $num_of_args;
echo "Parameter " . $loop . " = " . func_get_arg ($loop);
This is an example, to show uncomplicated values, but you can pass any type you want, just deal with them appropriately inside it.
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho