PHP func_get_arg() Function
Last Updated :
09 Jul, 2020
Improve
The func_get_arg() function is an inbuilt function in PHP which is used to get a mentioned value from the argument passed as the parameters.
Syntax:mixed func_get_arg( int $arg )
Parameters: This function accepts a single parameter as mentioned above and described below.
- $arg: This parameter holds the argument offset where the offset of the arguments in the parameter is counted by assuming the first argument to be 0.
Return Value: This method returns the mentioned argument and returns FALSE if an error occurs.
Example 1:<?php
// Function definition
function geeks($a, $b, $c) {
// Calling func_get_arg() function
echo "Print second argument: "
. func_get_arg(1) . "\n";
}
// Function call
geeks('hello', 'php', 'geeks');
?>
<?php
// Function definition
function geeks($a, $b, $c) {
// Calling func_get_arg() function
echo "Print second argument: "
. func_get_arg(1) . "\n";
}
// Function call
geeks('hello', 'php', 'geeks');
?>
Print second argument: php
When does any error occur? The error occurs in two cases.
- If the value of argument offset is more than the actual value of arguments passed as the parameter of the function.
- If this function is not being called from within the user-defined function.
<?php
// Function definition
function geeks($a, $b, $c) {
// Printing the sixth argument
// that doesn't exist
echo "Printing the sixth argument: "
. func_get_arg(5) . "\n";
}
// Function call
geeks('hello', 'php', 'geeks');
?>
<?php
// Function definition
function geeks($a, $b, $c) {
// Printing the sixth argument
// that doesn't exist
echo "Printing the sixth argument: "
. func_get_arg(5) . "\n";
}
// Function call
geeks('hello', 'php', 'geeks');
?>
Warning: func_get_arg(): Argument 5 not passed to function in [...][...] on line 4Example:
<?php
// Function definition
function geeks($a, $b, $c) {
$a = "Bye";
}
// Function call
geeks('hello', 'php', 'geeks');
// The func_get_arg() function
// is called from outside the
// user defined function
echo "Printing the sixth argument: "
. func_get_arg(5) . "\n";
?>
<?php
// Function definition
function geeks($a, $b, $c) {
$a = "Bye";
}
// Function call
geeks('hello', 'php', 'geeks');
// The func_get_arg() function
// is called from outside the
// user defined function
echo "Printing the sixth argument: "
. func_get_arg(5) . "\n";
?>
PHP Warning: func_get_arg(): Called from the global scope - no function context in /home/main.php on line 9For versions before PHP 5.3: Getting a function's argument has a different approach for the PHP versions below 5.3. All the versions above 5.3 and 5.3 will show an error for the following code. Example:
<?php
function geeks() {
include './testing.inc';
}
geeks('Welcome', 'PHP', 'Geeks');
?>
<?php
$parameter = func_get_arg(1);
var_export($parameter);
?>
'PHP' warningsNote: For getting more than one argument func_get_args() function can be used instead of func_get_arg() function.