Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
counting the number if params passed to function

Posted by John_Moylan (jfp), 9 June 2003
Hello All

Simple 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.


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:
     $highest = getbiggestpropn(array(10,30,60,40,20,19,$k,12,14));

and then your code for the function could read:
    function getbiggestpropn($of) {
         $total = 0;
         for ($k=0; $k<count($of); $k++) {
              $total += $of[$k];
         return ($total / count($of));

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.


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()
 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.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho