PHP func_get_args() Function
The PHP func_get_args() function returns an array comprising a function's argument list.
This function may be used in conjunction with func_get_arg() and func_num_args() to allow user-defined functions to accept variable-length argument lists.
Syntax
func_get_args()
Parameters
No parameter is required.
Return Value
Returns an array in which each element is a copy of the corresponding member of the current user-defined function's argument list.
Exceptions
Generates a warning if called from outside of a user-defined function.
Example: func_get_args() example
The example below shows the usage of func_get_args() function.
<?php function myFunction() { //getting the number of arguments passed $numargs = func_num_args(); echo "Number of arguments: $numargs\n"; //displaying the second argument if ($numargs >= 2) { echo "Second argument is: ".func_get_arg(1)."\n"; } //displaying the array containing //function's argument list print_r(func_get_args()); } myFunction(10, 20, 30); ?>
The output of the above code will be:
Number of arguments: 3 Second argument is: 20 Array ( [0] => 10 [1] => 20 [2] => 30 )
Example: func_get_args() example of byref and byval arguments
Consider the example below which demonstrates the effect of passing arguments by value and by reference while using this function.
<?php function byVal($arg) { echo 'As passed : ', var_export(func_get_args()), PHP_EOL; $arg = 'World'; echo 'After change : ', var_export(func_get_args()), PHP_EOL; } function byRef(&$arg) { echo 'As passed : ', var_export(func_get_args()), PHP_EOL; $arg = 'World'; echo 'After change : ', var_export(func_get_args()), PHP_EOL; } $x = 'Hello'; byVal($x); byRef($x); ?>
The output of the above code will be:
As passed : array ( 0 => 'Hello', ) After change : array ( 0 => 'World', ) As passed : array ( 0 => 'Hello', ) After change : array ( 0 => 'World', )
❮ PHP Function Handling Reference