【问题标题】:PHP is_null: Only boolean true and false, not even null?PHP is_null:只有布尔真假,甚至没有null?
【发布时间】:2013-04-19 07:48:53
【问题描述】:

跟进question and answer,我决定只接受布尔值 true 和 false,甚至接受来自其他开发者/用户输入的 null

$default = array(
    "category_id"   =>  null,
    "category"      =>  false,
    "randomise"     =>  false
);

$config = array(
    "category_id"   =>  17,
    "randomise"     =>  false, 
    "category"      =>  null
);

function process_array($default,$config)
{
    # Set empty arrays for error & items.
    $error = array();
    $items = array();

    # Loop the array.
    foreach($default as $key => $value)
    {
        if (is_bool($default[$key]) && isset($config[$key])) 
        {
            if ($config[$key] === null) $error[] = '"'. $key.'" cannot be null.';

            # Make sure that the value of the key is a boolean.
            if (!is_bool($config[$key])) 
            {
                $error[] = '"'. $key.'" can be boolean only.';
            }

        }

            if(isset($config[$key]) && !is_array($value))
            {
                $items[$key] = $config[$key];
            }
            elseif(isset($config[$key]) && is_array($value))
            {
                $items[$key] = array_merge($default[$key], $config[$key]);
            }
            else
            {
                $items[$key] = $value;
            }
        }

        # Give a key to the error array.
        $error = array("error" => $error);

        # Merge the processed array with error array.
        # Return the result.
        return array_merge($items,$error);
}

print_r(process_array($default,$config));

但我得到的结果是,

Array
(
    [category_id] => 17
    [category] => 
    [randomise] => 
    [error] => Array
        (
        )

)

我追求的结果,

Array
(
    [category_id] => 17
    [category] => 
    [randomise] => 
    [error] => Array
        (
         [0] => "category" cannot be null.
        )

)

所以我认为下面的这条线应该可以工作,但我不明白为什么它不起作用。我尝试使用is_null,但仍然无法正常工作。知道我做错了什么吗?我该如何解决这个问题?

if ($config[$key] === null) $error[] = '"'. $key.'" cannot be null.';

【问题讨论】:

    标签: php boolean isnull


    【解决方案1】:

    我相信null 的值不会通过if (is_bool($default[$key]) && isset($config[$key])) 中的isset() 测试,因此它会跳过整个块。

    我认为你需要进行一些重构来解决这个问题。或许可以从 if 中取出 isset 并将其移至 null 测试?

    if (!isset($config[$key]) || is_null($config[$key])) $error[] = '"'. $key.'" cannot be null.';

    【讨论】:

    • 谢谢 Bulk,我接受了 Jordy 的回答,效果很好!谢谢!
    【解决方案2】:

    isset($config[$key]) 如果值为 null,则返回 false。请改用array_key_exists (http://php.net/manual/function.array-key-exists.php)。

    【讨论】:

      【解决方案3】:

      null 也不会通过 is_bool 检查...据我所知 - 当涉及到 if statements - 最好尽可能简单:

      if (is_null($default[$key]))
      {
        $error[] = '"'. $key.'" cannot be null.';
      }
      else if (!is_bool($default[$key])) 
      {
        $error[] = '"'. $key.'" can be boolean only.';
      }
      

      正如另一位发帖人所说,最好将上述内容包含在array_key_exists 中,以避免出现非法偏移警告。 Tbh,为了简单起见,您真的需要这两项检查吗?指定 key 只能是布尔值就足够了。

      【讨论】:

      • 谢谢你的解释,pebbl。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 2021-03-01
      相关资源
      最近更新 更多