Friday, October 24, 2025
HomeLanguagesPHP func_get_arg() Function

PHP func_get_arg() Function

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 neveropen($a, $b, $c) {
  
    // Calling func_get_arg() function  
    echo "Print second argument: "
        . func_get_arg(1) . "\n";
}
  
// Function call
neveropen('hello', 'php', 'neveropen');
  
?>


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 neveropen($a, $b, $c) {
  
    // Printing the sixth argument
    // that doesn't exist
    echo "Printing the sixth argument: "
        . func_get_arg(5) . "\n";
}
  
// Function call
neveropen('hello', 'php', 'neveropen');
  
?>


Output:

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

Example:




<?php
  
// Function definition
function neveropen($a, $b, $c) {
     $a = "Bye";
}
  
// Function call
neveropen('hello', 'php', 'neveropen');
  
// 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 neveropen() {
    include './testing.inc';
}
  
neveropen('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.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS