The func_num_args Function

The func_num_args function allows you to get the number of arguments passed to the current user-defined function. It works only inside functions and does not require parameters.

Syntax

phpfunc_num_args();

Example

Let's create a function and output the number of arguments passed:

php<?php
  function testArgs() {
    echo func_num_args();
  }
  testArgs(1, 2, 3);
?>

Code execution result:

php3

Example

Let's check the number of arguments in a function without parameters:

php<?php
  function noArgs() {
    echo func_num_args();
  }
  noArgs();
?>

Code execution result:

php0

Example

Usage with func_get_args to handle a variable number of arguments:

php<?php
  function sumAll() {
    $count = func_num_args();
    $args = func_get_args();
    $sum = 0;
    
    for ($i = 0; $i < $count; $i++) {
      $sum += $args[$i];
    }
    
    return $sum;
  }
  
  echo sumAll(1, 2, 3, 4);
?>

Code execution result:

php10

See Also

  • the func_get_args function,
    which returns an array of passed arguments
  • the func_get_arg function,
    which returns a specific argument by index