PHP函数定义,空参数

Sjw*_*ies 3 php function

构造函数的最佳方法是什么,参数可以为null?

例如

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

        Do something with the variables

    }

# newDate('12', '', 'yes');
Run Code Online (Sandbox Code Playgroud)

是否只需按如下方式重构功能:

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

        Do something with the variables

    }

# newDate('12', 'yes');
Run Code Online (Sandbox Code Playgroud)

pgb*_*pgb 5

实际上,我认为你没有选择.

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

请参阅PHP 有关函数参数文档.