PHP variable number of parameter lists

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.