How do you create optional arguments in php?

后端 未结 6 1693
耶瑟儿~
耶瑟儿~ 2020-11-27 02:54

In the PHP manual, to show the syntax for functions with optional parameters, they use brackets around each set of dependent optional parameter. For example, for the date()

6条回答
  •  孤街浪徒
    2020-11-27 03:49

    The default value of the argument must be a constant expression. It can't be a variable or a function call.

    If you need this functionality however:

    function foo($foo, $bar = false)
    {
        if(!$bar)
        {
            $bar = $foo;
        }
    }
    

    Assuming $bar isn't expected to be a boolean of course.

提交回复
热议问题