在 PHP 函数中传递可选参数

php
2022-08-30 13:52:05

如何将可选参数传递给 PHP 函数。例如

function test($required, $optional){..}

因此,我应该能够通过传递一个参数或两个参数来调用该函数。例如:

test($required, $optional)
test($required);

谢谢。


答案 1

试试这个:

function test($required, $optional = NULL){..} 

然后你可以打电话

test($required, $optional)

和空值$optional

test($required);  

答案 2

使用默认值

function test($required, $optional="default value"){..}

推荐