不确定参数数量的自定义函数

<?php
function my_fun()
{
    $numArgs = func_num_args();
    echo "共接收到{$numArgs}个参数。" . PHP_EOL;

    // 获得所有参数并遍历输出
    $args = func_get_args();
    foreach ($args as $key => $value)
    {
        $num = $key + 1;
        echo "第{$num}个参数的值为:{$value}" . PHP_EOL;
    }
}

my_fun('张三', 18, '汉子');
/********** 输出结果·开始 **********
共接收到3个参数。
第1个参数的值为:张三
第2个参数的值为:18
第3个参数的值为:汉子
********** 输出结果·结束 **********/

Copyright © 2023 码农人生. All Rights Reserved