• PHP Video Tutorials

PHP - func_get_arg()



The func_get_arg() function can return an item from an argument list.

Syntax

mixed func_get_arg( int $arg_num )

The func_get_arg() function can return an argument that is at the arg_num'th offset into the user-defined function's argument list. The function arguments are counted starting from zero. This function can generate a warning if called from outside of function definition.

If the "arg_num" is greater than the number of arguments actually passed, a warning can be generated, and func_get_arg() can return false.

Example 1

<?php
   function printValue($value) {
      // Update value variable
      $value = "The value is: " . $value;

      // Print the value of the first argument
      echo func_get_arg(0);
   }
   // Run function
   printValue(123);
?>

Output

The value is: 123

Example 2

<?php
   function printValue($value) {
      $modifiedValue = $value + 1;
   
      echo func_get_arg(0);
   }
   printValue(1);
?>

Output

1

Example 3

<?php
   function some_func($a, $b) {
      for($i = 0; $i < func_num_args(); ++$i) {
         $param = func_get_arg($i);
         echo "Received parameter $param.\n";
      }
   }
	
   some_func(1,2,3,4,5,6,7,8);
?>

Output

Received parameter 1.
Received parameter 2.
Received parameter 3.
Received parameter 4.
Received parameter 5.
Received parameter 6.
Received parameter 7.
Received parameter 8.
php_function_reference.htm
Advertisements