gpt4 book ai didi

PHP 函数定义,空参数

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

构造一个参数可以为空的函数的最佳方式是什么?

例如

    function newDate($day, $time = null, $overRide) {

Do something with the variables

}

# newDate('12', '', 'yes');

是否可以简单地重构函数如下:

    function newDate($day, $overRide, $time = null) {

Do something with the variables

}

# newDate('12', 'yes');

最佳答案

实际上,我认为您别无选择。

默认参数应该是您函数中的最后一个参数。否则,当您调用 newDate(1, 2) 时,PHP 会将其转换为 newDate(1, 2, null),而您可能需要 newDate(1 , 空, 2).

参见 PHP 的 documentation on function arguments .

关于PHP 函数定义,空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2265654/

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