PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Variable-Length Argument Lists

This section provides a tutorial example on how to access arguments in a function without using argument variables and how to pass arguments more than what are defined in the function statement.

As I mentioned earlier, PHP also allows you to access arguments provided in the function call operation without using argument variables. Here is how:

1. func_get_arg(position) - It returns the value of the specified argument provided in the function call operation. "position" is the position index of the specified argument. The position index of the first argument is 0. For example, if "func_get_arg(2)" is used in a function, it will return the value of the 3rd argument.

2. func_num_args() - It returns the total number of arguments provided in the function call operation. For example, if "func_num_args()" returns 1, you know that there is only 1 argument provided by calling code.

3. func_get_args() - It creats and returns an array which contains values of all arguments provided in the function call operation. For example, if "$args = func_get_args()" is used in a function, $args will be array containing all arguments.

With all these 3 functions, we say that PHP supports variable-length argument lists with some basic rules:

  • The function call can provide more arguments than the number of argument variables defined in the function statement.
  • You can pass arguments to a function that has no argument variable defined.

To help us understand how use variable-length argument list feature, I wrote this tutorial example:

<?php # ArgumentList.php
# Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
#
   function f2c($fahrenheit) {
      $celsius = (func_get_arg(0) - 32.0) / 1.8;
      return $celsius;
   }

   print("\n Calling a function that uses func_get_arg():\n");
   if (f2c(20.0)<0.0) print("    It's cold here!\n");
   
   function myMax() {
      $max = NULL; 
      if (func_num_args()>0) $max = func_get_arg(0);
      for ($i=1; $i<func_num_args(); $i++) {
         if ($max < func_get_arg($i)) $max = func_get_arg($i);
      }
      return $max;
   }

   print("\n Calling a function that uses func_num_args():\n");
   print("    ".myMax()."\n");
   print("    ".myMax(3)."\n");
   print("    ".myMax(3, 1, 4)."\n");
   print("    ".myMax(3, 1, 4, 1, 5, 9, 2, 6, 5)."\n");

   function myMin() {
      $list = func_get_args();
      $min = NULL;
      if (count($list)>0) $min = $list[0];
      for ($i=1; $i<count($list); $i++) {
         if ($min > $list[$i]) $min = $list[$i];
      }
      return $min;
   }

   print("\n Calling a function that uses func_get_args():\n");
   print("    ".myMin()."\n");
   print("    ".myMin(5)."\n");
   print("    ".myMin(5, 6, 2)."\n");
   print("    ".myMin(5, 6, 2, 9, 5, 1, 4, 1, 3)."\n");
?>

If you run this sample script, you should get:

 Calling a function that uses func_get_arg():
    It's cold here!

 Calling a function that uses func_num_args():

    3
    4
    9

 Calling a function that uses func_get_args():

    5
    2
    1

Last update: 2005.

Sections in This Chapter

What Is a Function?

"function" Statements - Defining Functions

Function Call Operations

Passing Arguments to Functions

Example of Passing Arguments by Values

Using Pass-by-Value Arguments for References

Example of Passing Arguments by References

Variable-Length Argument Lists

Providing Default Values to Argument Variables

Returning Values from Functions

Returning References from Functions

Dr. Herong Yang, updated in 2009
Variable-Length Argument Lists