PHP func_get_arg() Function
Last Updated :
09 Jul, 2020
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 geeks( $a , $b , $c ) {
echo "Print second argument: "
. func_get_arg(1) . "\n" ;
}
geeks( 'hello' , 'php' , 'geeks' );
?>
|
Output:
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 geeks( $a , $b , $c ) {
echo "Printing the sixth argument: "
. func_get_arg(5) . "\n" ;
}
geeks( 'hello' , 'php' , 'geeks' );
?>
|
Output:
Warning: func_get_arg(): Argument 5 not passed to function in
[...][...] on line 4
Example:
<?php
function geeks( $a , $b , $c ) {
$a = "Bye" ;
}
geeks( 'hello' , 'php' , 'geeks' );
echo "Printing the sixth argument: "
. func_get_arg(5) . "\n" ;
?>
|
Output:
PHP Warning: func_get_arg(): Called from the global scope -
no function context in /home/main.php on line 9
For 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' );
?>
|
testing.inc:
<?php
$parameter = func_get_arg(1);
var_export( $parameter );
?>
|
Output:
'PHP' warnings
Note: For getting more than one argument func_get_args() function can be used instead of func_get_arg() function.
Share your thoughts in the comments
Please Login to comment...