Creating Variable-Length Argument Lists


Here's another thing you can do with functions in PHPyou can pass a variable number of arguments to functions. This is not the same as setting up default arguments; in this case, you can call the same function with a different number of arguments, and you can retrieve all the arguments using special functions instead of giving each argument a default value. You can pass as many arguments as you want. For example, if you have a function named joiner that joins string together, you might call it like this:

 joiner("No", "worries"); joiner("No", "worries", "here."); joiner("Here's", "a", "longer", "string."); 

In the joiner function, you can use three PHP functions to get the number of arguments passed to you, a single argument that you specify by number, and an array that holds all the arguments passed to you. Here are those functions:

  • func_num_args. Returns the number of arguments passed

  • func_get_arg. Returns a single argument

  • func_get_args. Returns all arguments in an array

Here's what the joiner function might look like using func_num_args to get the number of arguments and func_get_args to get all the passed arguments in an array:

 function joiner() {     $text = "";     $arg_list = func_get_args();     for ($loop_index = 0;$loop_index < func_num_args(); $loop_index++) {         $text .= $arg_list[$loop_index] . " ";     }     echo $text; } 

Now you can call this function as shown in phpvariableargs.php, Example 4-5.

Example 4-5. Passing data by reference, phpvariableargs.php
 <HTML>         <HEAD>             <TITLE>Using variable-length argument lists</TITLE>         </HEAD>         <BODY>             <H1>Using variable-length argument lists</H1>             <?php             echo "joiner(\"No\", \"worries\") =  ", joiner("No", "worries"),                  "<BR>";             echo "joiner(\"No\", \"worries\", \"here.\") =  ", joiner("No",                  "worries", "here."), "<BR>";             echo "joiner(\"Here's\", \"a\", \"longer\", \"string.\") =  ",                  joiner("Here's", "a", "longer", "string."), "<BR>";             function joiner()             {                 $text = "";                 $arg_list = func_get_args();                 for ($loop_index = 0;$loop_index < func_num_args();                      $loop_index++) {                     $text .= $arg_list[$loop_index] . " ";                 }                 echo $text;             }             ?>     </BODY> </HTML> 

The results appear in Figure 4-5, where we've been able to get all the arguments passed to uscool.

Figure 4-5. Creating functions with variable-length argument lists.


You can also use the func_get_arg function to get a single argument; just pass it the (0-based) position of the argument you want, and it will return that argument's value.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

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