Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP func_get_args() function

Reference for PHP func_get_args(), func_get_arg() and func_num_args(): read the arguments passed to a function, including extra ones, and why variadics replace them.

func_get_args() returns an array with every argument passed to the current function, including extras beyond the declared parameters. func_num_args() returns how many were passed, and func_get_arg($position) returns the one at a zero-based position.

These functions are the old way to write "any number of arguments" functions. They still work and are useful for debugging or for wrappers, but a typed variadic parameter (...$args) is clearer and safer in new code.

PHP
function sum()
{
    return array_sum(func_get_args());
}
echo sum(1, 2, 3); // 6

Try it

PHP Reading all arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Variadic replacement and errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
func_get_args(): array
func_get_arg(int $position): mixed
func_num_args(): int
Name Description
$position Zero-based index of the argument to return. Must be less than the number of arguments passed.

Return value: func_get_args() returns a list array of all passed arguments. func_get_arg() returns one argument. func_num_args() returns the count.

Errors: calling any of them outside a function throws Error. func_get_arg() with a negative position or one beyond the passed count throws ValueError in PHP 8.

Usage notes

  • Count is what was passed, not declared. A default value that was not supplied does not appear in func_get_args().
  • Values reflect modification (PHP 7+). If you change a parameter variable before calling func_get_args(), the changed value is returned. In older versions you always got the original.
  • Extras are kept. Arguments beyond the declared list are still returned, which is what made the pattern useful for variadic behavior.
  • Types are not enforced on extras. Declared parameters are type-checked; surplus arguments are not.
  • Prefer ...$args. A variadic parameter documents intent, allows a type, and works with static analysis and named arguments. func_get_args() cannot be seen from the signature.
  • As an argument. Since PHP 5.3, func_get_args() can be used directly in a call like array_sum(func_get_args()).
  • Not for methods called through magic: in __call($name, $args) you already receive the arguments array.
  • Debugging wrappers: a wrapper that forwards all arguments can use call_user_func_array($target, func_get_args()), though ...$args is neater.