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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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


and then your code for the function could read:
Code:
    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.

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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho