【问题标题】:ternary operator for a variable变量的三元运算符
【发布时间】:2017-09-25 12:31:58
【问题描述】:

我有一个日期字段,它为空值返回“1970-01-01”。所以我想为变量$from创建一个三元运算符。它仍然返回1970-01-01。我做错了什么

$from=$asset_other_details->start_date;  //1970-01-01
$from == '1970-01-01' ? '' : $from ;

【问题讨论】:

  • 检查我给出的确切文本
  • 你没有重新分配from
  • 在作业中做。
  • 你的陈述中根本没有条件。
  • @iainn 为什么不呢?它只是if 的简写,通常也是这样使用的。 (参见手册页上的示例)

标签: php datetime


【解决方案1】:

您必须根据如下比较输出为$form 分配新值:-

$from = $asset_other_details->start_date;  //1970-01-01
$from = ($from == '1970-01-01') ? '' : $from ;

要理解的输出:- https://eval.in/867743

【讨论】:

    【解决方案2】:

    您没有为 $from 分配新值。请尝试以下操作:

    $from = ($asset_other_details->start_date == '1970-01-01') ? '' : $asset_other_details->start_date;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      相关资源
      最近更新 更多