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
func_num_args();
Example
Let's create a function and output the number of arguments passed:
<?php
function testArgs() {
echo func_num_args();
}
testArgs(1, 2, 3);
?>
Code execution result:
3
Example
Let's check the number of arguments in a function without parameters:
<?php
function noArgs() {
echo func_num_args();
}
noArgs();
?>
Code execution result:
0
Example
Usage with func_get_args to handle a variable number of arguments:
<?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:
10
See Also
-
the
func_get_argsfunction,
which returns an array of passed arguments -
the
func_get_argfunction,
which returns a specific argument by index