gpt4 book ai didi

php - 如何在PHP中实现一个可以接受任意参数的函数?

转载 作者:可可西里 更新时间:2023-11-01 00:53:31 26 4
gpt4 key购买 nike

PHP有这样的功能吗?

最佳答案

您可以使用这些功能:

例子:

function my_sum() { // <--- No parameters! :D
$total = func_num_args();
$numbers = func_get_args();

if ($total < 1) {
trigger_error('Less than one number :(');
return 0;
} else {
// Calculate the sum
$sum = array_sum($numbers);
return ($sum / $total);
}
}

// Usage:
echo my_sum(1, 2, 5, 109, 10231);
?>

关于php - 如何在PHP中实现一个可以接受任意参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2151941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com