Open In App

PHP func_get_arg() Function

Last Updated : 09 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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');
  
?>


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 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');
  
?>


Output:

Warning:  func_get_arg():  Argument 5 not passed to function in 
    [...][...] on line 4

Example:




<?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";
          
?>


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads