func_get_args

(PHP 4, PHP 5)

func_get_args --  Returns an array comprising a function's argument list

Description

array func_get_args ( void )

Returns an array in which each element is a copy of the corresponding member of the current user-defined function's argument list. func_get_args() will generate a warning if called from outside of a function definition. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function.

注: This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.

注: 因为本函数依赖于当前域来决定参数细节,因此不能用作函数参数。如果必须要传递这个值,将结果赋给一个变量,然后传递该变量。

<?php
function foo()
{
    
$numargs = func_num_args();
    echo
"Number of arguments: $numargs<br />\n";
    if (
$numargs >= 2) {
        echo
"Second argument is: " . func_get_arg(1) . "<br />\n";
    }
    
$arg_list = func_get_args();
    for (
$i = 0; $i < $numargs; $i++) {
        echo
"Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
}

foo(1, 2, 3);
?>

func_get_args() may be used in conjunction with func_num_args() and func_get_arg() to allow user-defined functions to accept variable-length argument lists.


add a note add a note User Contributed Notes
01-Nov-2006 02:26
Same idea as below, but this:

       foreach( $args as $k => $v ){
           switch($k){
               case 'a':
                   $this->a= $v; break;
               case 'b':
                   $this->b= $v; break;
               case 'c':
                   $this->c= $v; break;
             }
       }

can be shortened to this (as long as all public variables have default values set in their declarations):

       foreach( $args as $k=>$v)
           if(isset($this->$k)) $this->$k = $v;
kidekat
01-Oct-2006 09:02
To pass named arguments in a Perl fashion through class constructors, I use this:

<?php
class Test{

  
// set class defaults for values not assigned by constructor
  
public $a = 0;
  
public $b = 'string';
  
public $c = array();

  
public function __construct( $args = array() ) {

      
// parse tagged arguments in Perl fashion
      
foreach( $args as $k => $v ){
           switch(
$k){
               case
'a':
                  
$this->a= $v; break;
               case
'b':
                  
$this->b= $v; break;
               case
'c':
                  
$this->c= $v; break;
             }
       }
   }

}

$t = new Test(  array( 'b'=>'new value', 'c'=>array(1,'test') ) );
?>

This allows $a to keep its default of 0, while $b gets reassigned to 'new value' and $c becomes the array(1,'test'). The catch is that you add O(n^2) "big-O notation" to the begging of every class constructor which becomes expensive on "larger" classes. While arguments defaults like the following have only O(n) "constant" amount of work.

<?php
   public funciton __construct
( $a=0, $b='string', $c=array()){ ... }
?>
bew
31-Mar-2006 11:55
A more concise way of expressing my idea from the previous post (I'd forgotten about array_slice()):

<?php
function func_get_default_args($a) {
  
$args = array_slice(func_get_args(), 1);
   return
array_merge($args, array_slice($a, sizeof($args)));
}

function
foo($a = 1, $b = 2, $c = 3) {
  
print_r(func_get_default_args(func_get_args(), $a, $b, $c));
}

// prints: Array ( [0] => a [1] => b [2] => 3 )
foo('a', 'b');
?>
Nathan Ostgard
07-Dec-2005 02:14
If you're using PHP5, the variable number of argument functions all return the objects by reference - and not a copy of the object, as this leads you to believe.
robert at defore dot st
15-Feb-2005 04:47
# Another attempt at named args (perl-inspired):

# list_to_assoc('key', 'value', 'key', 'value', ...) =>
#    pairs[]
function list_to_assoc() {
   $list = func_get_args();
   $assoc = array();

   while ($list and count($list) > 1) {
       $assoc[array_shift($list)] = array_shift($list);
   }

   if ($list) { $assoc[] = $list[0]; }

   return $assoc;
}

# Usage:

function example($required) {
   $args = func_get_args(); array_shift($args); # drop 'required'
   $rest = list_to_assoc($args);
   echo "$required\n" . $rest['comment'];
}

example("This is required...",
       'comment', 'this is not.'); # this is like 'comment' => 'this is not'
T.M.
04-Nov-2004 11:24
Simple function to calculate average value using dynamic arguments:
<?php
function average(){
   return
array_sum(func_get_args())/func_num_args();
}
print
average(10, 15, 20, 25); // 17.5
?>
volte6 at drunkduck dot com
01-Oct-2004 05:54
For those who have a use for a C style enum() function:

//*******************************************
// void enum();
// enumerates constants for unique values guarenteed.
function enum()
{
  $i=0;
  $ARG_ARR = func_get_args();
  if (is_array($ARG_ARR))
  {
   foreach ($ARG_ARR as $CONSTANT)
   {
     define ($CONSTANT, ++$i);
   }
  }
}

// USAGE:
enum(ERR_USER_EXISTS, ERR_OLD_POST);

// etc. etc.
//*******************************************

this can be used for error codes etc.
I deliberately skipped the 0 (zero) define, which could be useful for error checking.
mark at manngo dot net
24-Mar-2003 03:13
You can also fake named arguments using eval:

function test()
{  foreach (func_get_args() as $k=>$arg) eval ("\$$arg;");
   echo "$a plus $b gives ".($a+$b);
}

test("a=3","b=4");
fbeyer at clickhand dot de dot noSpamPlease
20-Jan-2002 12:02
Another way of passing references with a dynamic number of arguments: (This example is limited to 10 arguments)

<?php
define
('NULL_ARG', 'DUMMY_ARGUMENT');

function
refArg($arg0 = NULL_ARG,
              
$arg1 = NULL_ARG,
              
$arg2 = NULL_ARG,
              
$arg3 = NULL_ARG,
              
$arg4 = NULL_ARG,
              
$arg5 = NULL_ARG,
              
$arg6 = NULL_ARG,
              
$arg7 = NULL_ARG,
              
$arg8 = NULL_ARG,
              
$arg9 = NULL_ARG)
{
   for (
$args=array(), $i=0; $i < 10; $i++) {
      
$name = 'arg' . $i;
       if (
$i < func_num_args()) {
          
$args[$i] = &$$name;
       }
       unset($
$name, $name);
   }

  
$args[0] = 'Modified.';
}

$test = 'Not modified.<br>';
refArg(&$test);

echo
$test; // Prints 'Modified'
?>
daveNO at ovumSPAMdesign dot com
18-Sep-2001 02:29
<?php
// How to simulate named parameters in PHP.
// By Dave Benjamin <dave@ovumdesign.com>

// Turns the array returned by func_get_args() into an array of name/value
// pairs that can be processed by extract().
function varargs($args) {
  
$count = count($args);
   for (
$i = 0; $i < $count; $i += 2) {
      
$result[$args[$i]] = $args[$i + 1];
   }
  
   return
$result;
}

// Example
function test(&$ref1, &$ref2) {
  
// Default arguments go here.
  
$foo = "oof";
  
  
// Do some magic.
  
extract(varargs(func_get_args()));

   echo
nl2br("\n\$var1 = $var1");
   echo
nl2br("\n\$var2 = $var2");
   echo
nl2br("\n\$foo = $foo\n\n");
  
  
// Modify some variables that were passed by reference.
   // Note that func_get_args() doesn't pass references, so they
   // need to be explicitly declared in the function definition.
  
$ref1 = 42;
  
$ref2 = 84;
}

$a = 5;
$b = 6;

echo
nl2br("Before calling test(): \$a = $a\n");
echo
nl2br("Before calling test(): \$b = $b\n");

// Try removing the 'foo, "bar"' from the following line.
test($a, $b, var1, "abc", var2, "def", foo, "bar");

echo
nl2br("After calling test(): \$a = $a\n");
echo
nl2br("After calling test(): \$b = $b\n");
?>
04-Jun-2001 10:44
You can pass a variable number of arguments to a function whilst keeping references intact by using an array. The disadvantage of course, is that the called function needs to be aware that it's arguments are in an array.
<?
// Prints "hello mutated world"
function mutator($args=null) {
$n=count($args);
while(
$i<$n) $args[$i++] = "mutated";
}
$a = "hello";
$b = "strange";
$c = "world";
mutator(array($a, &$b, $c));
echo
"$a $b $c";
?>