Php 未设置cookie覆盖方法参数

Php 未设置cookie覆盖方法参数,php,Php,因此,我有一种方法可以将时区转换为另一个时区: public function timeZoneConverter($from, $to = "America/New_York", $outputformat){ $time = new DateTime($from, new DateTimeZone('America/New_York')); $time->setTimezone(new DateTimeZone($to)); return

因此,我有一种方法可以将时区转换为另一个时区:

public function timeZoneConverter($from, $to = "America/New_York", $outputformat){
        $time = new DateTime($from, new DateTimeZone('America/New_York'));
        $time->setTimezone(new DateTimeZone($to));
        return $time->format($outputformat);
    }
我从cookie中给出
$to
参数值,如下所示:

$calendar->timeZoneConverter($episode->air_date, $_COOKIE['timezone'], "Y-m-d H:i:s")
但是如果没有设置cookie,
$to
参数返回NULL,尽管我将“America/New_York”设置为默认值

为什么??cookie的空值是否正在覆盖它?我可以这样硬编码:

if($to == null){
   $to = "America/New_York";
}

但这似乎有点愚蠢。

这就是你必须要做的。将
null
传递给可选参数不会导致PHP返回默认值,它只会使用传递的
null

此外,在可选参数列表中间放置可选参数是没有意义的。如果不将某些值传递给

$to
,则无法传递所需参数
$outputformat

您可以删除默认值,并使用简短的合并语法来处理
null
案例:

$time->setTimezone(new DateTimeZone($to ?: 'America/New_York'));

您是否尝试转储cookie值?有时cookie会保留,并且是一个空字符串,而不是null,因此请尝试
if($to==null | |$to==“”)