func_num_argsReturnsThe number of arguments passed to a user function; -1 if func_get_args() is called in global scope Descriptionfunc_num_args() returns the number of arguments that have been passed to a user-defined function. It's most often used in conjunction with func_get_arg() and func_get_args() to ensure that the right number of arguments have been passed to a function. ExampleExample 382. Demonstrate how func_num_args() works <pre> <?php function demo () { $num_args = func_num_args(); print "The function was passed $num_args argument(s).\n"; } demo (); demo ('A single argument'); demo ('multiple', 'arguments'); demo (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20); ?> </pre> Example 383. Show how func_num_args() and func_get_arg() can work together <pre> <?php // Duplicate MySQL's interval() function // Note the use of argument prototyping to ensure that // if the function is called with less than the two required // arguments, an error will be thrown. function mysql_interval ($test_value, $interval_start) { if ($test_value === NULL) return -1; $num_args = func_num_args (); // Loop through the argument list - starting after the first argument for ($index = 1; $index < $num_args; ++$index) { // Get the value of the current item in the list $current_value = func_get_arg ($index); // Try to find the first item that the test value is less than if ($test_value < $current_value && ! isset ($return_value)) $return_value = $index - 1; // Make sure that the arguments match the proper format for the function if (! is_numeric ($current_value) || $current_value <= $last_value && isset ($last_value)) { trigger_error ('The second and subsequent arguments to this function must be numbers '. "that increase in value from left to right.\n i.e. arg 1 < arg 2 < arg 3 ...", E_USER_WARNING); return FALSE; } $last_value = $current_value; } // If $test_value was less than one of the items in the argument list if (isset ($return_value)) return $return_value; // If $test_value was not less than any of the other arguments return $index - 1; } print mysql_interval (1.5, 0, 1, 2, 3) . "\n"; print mysql_interval (9, 1, 2, 4, 8) . "\n"; print mysql_interval (9, -1, -2, -1, 4) . "\n"; ?> </pre>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|