PHP print_r写变量的玄机?
PHP print_r writes variable mystery?
我 运行 在我正在使用的网络客户端中 php 遇到了一些非常奇怪的行为。本质上,我将过期日期作为字符串传递,我将其解析为 DateTime,因此我可以将它与当前时间进行比较(以确定是否已过期。)
这是有问题的代码片段(注意:$expiration 设置在该片段上方,我只是用我实际打算使用的值重写它)
$expiration = DateTime::createFromFormat("y-m-d h:i:s", $expiration);
$now = date('y-m-d h:i:s', time());
Common::log("before : ", $expiration->date);
//TODO figure out why this common log has to be here or this doesnt work
Common::log("expiration : ", $expiration);
Common::log("after : ", $expiration->date);
if($now > $expiration->date) $data['status'] = 14;
Common::log 是一个内部函数,它只是在做 print_r
static function log ($msg, $data=null)
{
error_log ($msg . ($data ? print_r ($data, true) : ''));
}
该代码片段输出到终端(这是我正在查看打印内容的地方)的内容如下。
[09-Jun-2015 17:03:19 America/Indiana/Indianapolis] before :
[09-Jun-2015 17:03:19 America/Indiana/Indianapolis] expiration : DateTime Object
(
[date] => 2015-06-09 06:16:55
[timezone_type] => 3
[timezone] => America/Indiana/Indianapolis
)
[09-Jun-2015 17:03:19 America/Indiana/Indianapolis] after : 2015-06-09 06:16:55
如果我只是注释掉或删除记录 $expiration
变量的行,那么,如 before :
日志所示,$expiration->date
计算为空字符串,我的逻辑执行下面的比较中断。这是怎么回事,为什么去掉中间那个log对$expiration->date
的值有影响?这非常令人困惑,我将不胜感激任何人可以提供的任何帮助——如果我不明白它为什么有效,我不想使用有效的代码。
老实说,我不确定为什么会这样。它可能与 class 构造函数有关。但是,没有以这种方式使用 ->date 的文档。所以,与其做这样的事情:
Common::log("before : ", $expiration->date);
这样做:
Common::log("before : ", $expiration->format('y-m-d h:i:s'));
换句话说,您是在告诉 PHP 以您选择的输出格式显示日期(这可能与您创建它时使用的输入格式不同)。
我 运行 在我正在使用的网络客户端中 php 遇到了一些非常奇怪的行为。本质上,我将过期日期作为字符串传递,我将其解析为 DateTime,因此我可以将它与当前时间进行比较(以确定是否已过期。)
这是有问题的代码片段(注意:$expiration 设置在该片段上方,我只是用我实际打算使用的值重写它)
$expiration = DateTime::createFromFormat("y-m-d h:i:s", $expiration);
$now = date('y-m-d h:i:s', time());
Common::log("before : ", $expiration->date);
//TODO figure out why this common log has to be here or this doesnt work
Common::log("expiration : ", $expiration);
Common::log("after : ", $expiration->date);
if($now > $expiration->date) $data['status'] = 14;
Common::log 是一个内部函数,它只是在做 print_r
static function log ($msg, $data=null)
{
error_log ($msg . ($data ? print_r ($data, true) : ''));
}
该代码片段输出到终端(这是我正在查看打印内容的地方)的内容如下。
[09-Jun-2015 17:03:19 America/Indiana/Indianapolis] before :
[09-Jun-2015 17:03:19 America/Indiana/Indianapolis] expiration : DateTime Object
(
[date] => 2015-06-09 06:16:55
[timezone_type] => 3
[timezone] => America/Indiana/Indianapolis
)
[09-Jun-2015 17:03:19 America/Indiana/Indianapolis] after : 2015-06-09 06:16:55
如果我只是注释掉或删除记录 $expiration
变量的行,那么,如 before :
日志所示,$expiration->date
计算为空字符串,我的逻辑执行下面的比较中断。这是怎么回事,为什么去掉中间那个log对$expiration->date
的值有影响?这非常令人困惑,我将不胜感激任何人可以提供的任何帮助——如果我不明白它为什么有效,我不想使用有效的代码。
老实说,我不确定为什么会这样。它可能与 class 构造函数有关。但是,没有以这种方式使用 ->date 的文档。所以,与其做这样的事情:
Common::log("before : ", $expiration->date);
这样做:
Common::log("before : ", $expiration->format('y-m-d h:i:s'));
换句话说,您是在告诉 PHP 以您选择的输出格式显示日期(这可能与您创建它时使用的输入格式不同)。