【问题标题】:how to change an error message to a custom one in php如何在php中将错误消息更改为自定义消息
【发布时间】:2019-04-08 14:16:42
【问题描述】:

我从 sql 查询中获取日期并将其传递给 php,有时我会收到此错误消息

警告:date_format() 期望参数 1 为 DateTimeInterface,null 中给出

这是因为某些字段为空,因为当字段为空时,我会更改该消息以显示“无日期记录”

<td><?=$fhi=date_format($key[9], 'Y/m/d');?></td>

【问题讨论】:

  • 您在尝试使用之前验证数据。如果验证失败,您将显示一条错误消息。 永远不要假设并非直接来自您的数据是有效的。

标签: php


【解决方案1】:

您可以(应该?)检查该值是否为null在尝试使用它之前。

<?= $fhi = isset($key[9]) ? date_format($key[9], 'Y/m/d') : ("No date record"); ?>

【讨论】:

    【解决方案2】:

    您可以使用三元运算符来查看值是否为空。如果$key[9] 为空,则显示其他消息 - 否则使用date_format()。

    <?= $fhi = $key[9] === null ? 'No date record' : date_format($key[9], 'Y/m/d'); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2014-10-15
      相关资源
      最近更新 更多