Home > Article > Backend Development > What are the three points of the php method?
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().
#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!