Variable number of parameter lists
PHP supports a variable number of parameter lists in user-defined functions. In PHP 5.6 and later, the syntax is implemented in PHP 5.5 and earlier, using Functions Func_num_args (),Func_get_arg (), and Func_get_args ().
...In PHP 5.6+
Case one:
After PHP 5.6, the parameter list can include ..., which indicates that the function accepts a variable number of arguments. The parameter is passed as an array to the given variable, for example:
<?php
function sum (... $numbers) {
$ACC = 0;
foreach ($numbers as $n) {
$ACC + = $n;
}
return $ACC;
}
echo sum (1, 2, 3, 4);
?>
What the output of the above program will be.
Case TWO:
You can also use ... When a function is called, the array or traversed variable or text is disassembled into the argument list: columns such as
<?php
function add ($a, $b) {
return $a + $b;
}
echo Add (... [1, 2]). " \ n ";
$a = [1, 2];
echo Add (... $a);
$a = [1, 2,3,4];
echo Add (... $a);
?>
What will the output of the above program be?
Case THREE:
You can find it in ... Before specifying the normal positional parameters. In this case, only trailing parameters that do not match the positional parameters will be added to the by ... In the generated array.
can also be in ... Before you add a type hint. If present, then by ... All parameters captured must be an object of the prompt class.
<?php
function Total_intervals ($unit, DateInterval ... $intervals) {
$time = 0;
foreach ($intervals as $interval) {
$time + = $interval $unit;
}
return $time;
}
$a = new DateInterval (' p1d ');
$b = new DateInterval (' p2d ');
Echo total_intervals (' d ', $a, $b). ' Days ';
This would fail, since null isn ' t a DateInterval object.
Echo total_intervals (' d ', null);
?>
The result of the above program output is
3 dayscatchable Fatal Error:argument 2 passed to Total_intervals () must is an instance of DateInterval, null given, Calle D in-on line + defined in-on Line 2
Basic knowledge of PHP
PHP variable number of parameter lists