【问题标题】:PHP filter_input: how to return NULL when it is empty from the inputPHP filter_input:当输入为空时如何返回NULL
【发布时间】:2023-03-08 19:44:02
【问题描述】:

当输入为空时,如何使用filter_input 返回NULL?

$title = filter_input(INPUT_POST, 'title');
var_dump($title);

它返回,

string '' (length=0)

但我想要,

null

谢谢。

【问题讨论】:

    标签: php filter null filter-input


    【解决方案1】:

    NULL 应该是 FILTER_NULL_ON_FAILURE。但这也不是你想要的。为什么需要它为NULL?

    您得到字符串 (0) 的原因是表单中的 post 字段可用。该字段是空的。如果您希望它为 NULL,则该字段不应存在。

    $foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING, array('options' => array('default' => NULL))); var_dump($foo);

    所以使用 strlen()。或者确保该字段在您不需要时不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      相关资源
      最近更新 更多