Home > Backend Development > PHP Problem > What are the three points of the php method?

What are the three points of the php method?

藏色散人
Release: 2023-03-13 22:38:01
Original
4260 people have browsed it

In php, three dots represent a variable number of parameter lists. In PHP 5.6 and above, it is implemented by... syntax, while in PHP 5.5 and earlier versions, it is used Implemented by the functions func_num_args(), func_get_arg() and func_get_args().

What are the three points of the php method?

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

What are the three points of the php method?

php Usage of three dots

Explanation: Variable number of parameter lists

In PHP 5.6 and above, it is implemented by... Syntax ;In PHP 5.5 and earlier versions, use the functions func_num_args(), func_get_arg(), and func_get_args() to implement

Official documentation: https://www.php.net/manual/zh/functions. arguments.php#functions.variable-arg-list

Case:

<?php
function sum(...$numbers) {
    $acc = 0;
    foreach ($numbers as $n) {
        $acc += $n;
    }
    return $acc;
}
echo sum(1, 2, 3, 4);
?>
Copy after login

Output of the above case: 10

<?php
function add($a, $b) {
    return $a + $b;
}
echo add(...[1, 2])."\n";
$a = [1, 2];
echo add(...$a);
?>
Copy after login

Output of the above case: 3 3 (The result is the same, two 3)

Summary: This function accepts a variable number of parameters. The parameters will be passed to the given variables as an array

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the three points of the php method?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template