Blogger Information
Blog 17
fans 0
comment 1
visits 14609
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php:不定参函数剩余参数的使用 - 2019.9.28
Alfred的博客
Original
1170 people have browsed it

剩余参数计算乘积:

php7+版本可以将函数传参的剩余参数组成数组:

实例

// 计算不定参,乘积
function product_test(...$params) {
	// print_r($params);
	$productNum = 1;
	$num = count($params);
	for ($i = 0; $i < $num; ++$i) {
		$productNum *= $params[$i];
	}
	return $productNum;
}
echo product_test(1, 2, 3, 4, 5, 6, 7, 8);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:echo product_test(1, 2, 3, 4, 5, 6, 7, 8); 建议修改为: echo product_test(...range(1,5)); 岂不更简洁
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments