如果函数中没有提供参数,我如何测试它是否留空?

tes*_*ter 1 php

<?php    
/* Copyright Date
--------------------------*/
function copyright_date($creation_year) {
    $current_year = date('Y');

    if ($creation_year == $current_year || $creation_year == '') {
        echo $current_year;
    } 
    else {
        echo $creation_year . '-' . $current_year;
    }           
}
?>
Run Code Online (Sandbox Code Playgroud)

如果有人忘记添加参数(网站创建的年份),例如

<?php copyright_date(); ?>
Run Code Online (Sandbox Code Playgroud)

代替:

<?php copyright_date(2009); ?>
Run Code Online (Sandbox Code Playgroud)

我将如何测试以查看参数是否留空?在我的 if 语句中,这就是 $creation_year == '' 的用途,但由于参数不是字符串,所以它不起作用。

Jer*_*ten 5

$creation_year用默认值的可选参数NULL

function copyright_date($creation_year=NULL) {
Run Code Online (Sandbox Code Playgroud)

现在在函数内部只测试是否$creation_year等于NULL,这将在不带参数调用函数时发生。