8.10 Fetching an Arbitrary Number of Parameters


You want to fetch an arbitrary number of parameters from 0 N “1, where N is the total number of parameters.

Technique

Use the func_get_args() function to return an array containing the arguments passed to the function:

 <?php $input_record_seperator = " "; function perl_print () {     $args = func_get_args();     foreach ($args as $arg) {         print $arg . $input_record_seperator;     } } perl_print("Hello World\n", "My Name is", "Sterling"); ?> 

Comments

PHP offers a set of handy functions for accepting an arbitrary amount of parameters. The func_get_args() function returns an array of all the function arguments passed to the script. As an alternative, you can also use the func_get_arg() and func_num_args() functions to loop through all the parameters sent to a function:

 <?php $input_record_separator = " "; function perl_print () {     $argc = func_num_args();     for ($idx = 0; $idx < $argc; $idx++) {         $current_arg = func_get_arg($idx);         print $current_arg . $input_record_separator;     } } ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net