函数如下:
function test($username, $is_active=1, $sent_email=1, $sent_sms=1) {
echo $sent_email; // It should print default ie 1
}
调用函数:
test($username, 1, null, 1);
如果需要在function中使用默认值,如何调用function。$sent_email应该是1。不能改变参数的顺序。
当你启动的值参数发生:
function makecoffee($type = "cappuccino")
{
return "Making a cup of $type.'n";
}
echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");
?>
上面的示例将输出:
Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
满足您的检查要求,条件如下:
function test($username, $is_active=1, $sent_email=1, $sent_sms=1) {
if($sent_email!=1)
$sent_email=1;
echo $sent_email; // It should print default ie 1
}
在php中,你不能在函数中声明超过1个默认值的参数。如果你有多个默认值,Php无法知道你没有给出哪个参数…
在您的例子中,您给出了参数,值为空。这是非常不同的!因此,您可以使用以下命令:
function test($username, $is_active, $sent_email, $sent_sms) {
$username = ($username != null) ? $username : 1;
$is_active = ($is_active != null) ? $is_active : 1;
$sent_email = ($sent_email != null) ? $sent_email : 1;
echo $sent_email; // It should print default ie 1
}
作为它,如果你给null,你的函数将使用"1"值,如果不是null,你作为参数传递的值;)