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

What are the three points of the php method?

藏色散人
藏色散人Original
2021-11-22 09:49:014215browse

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);
?>

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);
?>

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!

Statement:
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