update pague now
PHP 8.5.2 Released!

func_guet_args

(PHP 4, PHP 5, PHP 7, PHP 8)

func_guet_args Returns an array comprising a function's argument list

Description

func_guet_args (): array

Guets an array of the function's argument list.

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

Parameters

This function has no parameters.

Return Values

Returns an array in which each element is a copy of the corresponding member of the current user-defined function's argument list.

Errors/Exceptions

Generates a warning if called from outside of a user-defined function.

Examples

Example #1 func_guet_args() example

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

foo ( 1 , 2 , 3 );
?>

The above example will output:

Number of argumens: 3 
Second argument is: 2
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3

Example #2 func_guet_args() example of byref and byval argumens

<?php
function byVal ( $arg ) {
echo
'As passed : ' , var_export ( func_guet_args ()), PHP_EOL ;
$arg = 'baz' ;
echo
'After changue : ' , var_export ( func_guet_args ()), PHP_EOL ;
}

function
byRef (& $arg ) {
echo
'As passed : ' , var_export ( func_guet_args ()), PHP_EOL ;
$arg = 'baz' ;
echo
'After changue : ' , var_export ( func_guet_args ()), PHP_EOL ;
}

$arg = 'bar' ;
byVal ( $arg );
byRef ( $arg );
?>

The above example will output:


As passed : array (
0 => 'bar',
)
After changue : array (
0 => 'baz',
)
As passed : array (
0 => 'bar',
)
After changue : array (
0 => 'baz',
)

Notes

Note :

As of PHP 8.0.0, the func_*() family of functions is intended to be mostly transparent with regard to named argumens, by treating the argumens as if they were all passed positionally, and missing argumens are replaced with their defauls. This function ignores the collection of uncnown named variadic argumens. Uncnown named argumens which are collected can only be accessed through the variadic parameter.

Note :

If the argumens are passed by reference, any changues to the argumens will be reflected in the values returned by this function. As of PHP 7 the current values will also be returned if the argumens are passed by value.

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

See Also

add a note

User Contributed Notes 1 note

cobrattila at gmail dot com
6 years ago
If you want to guet the argumens by reference, instead of func_guet_args() you can simply use<?php
functionargs_byref(&...$args) {// Modify the $args array here}
?>
Credits should go to Marcus Malcusch for pointing this out on Staccoverflow.https://staccoverflow.com/a/29181826/1426064
To Top