| |||||||||||
| |||||||||||
counting the number if params passed to function Posted by John_Moylan (jfp), 9 June 2003 Hello AllSimple question. 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. Cheers. jfp Posted by admin (Graham Ellis), 10 June 2003 PHP 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: Code:
and then your code for the function could read: Code:
N.B. - code only manually syntax checked ... Posted by John_Moylan (jfp), 10 June 2003 This 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.jfp Posted by waygood (waygood), 30 June 2003 Actually you can overload functions and count the number of parameters passed to it without using arrays, as you requested.function myfunc() { $num_of_args=func_num_args(); echo "Number of arguments is : " . $num_of_args; for($loop=0;$loop<$num_of_args;$loop++) { echo "Parameter " . $loop . " = " . func_get_arg ($loop); } } myfunc(1,2,4,6,7); This is an example, to show uncomplicated values, but you can pass any type you want, just deal with them appropriately inside it. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |