区分未设置AND null?

时间:2015-06-13 13:33:12

标签: php null

$record['field'] = null;

if (isset($record['field']) || is_null($record['field'])) {
  // When $record['field'] is set and not null, OR, when $record['field'] = null
  // Basically, when $record['field'] is defined as $record['field'] = whatever, even null.
} else {
  // When $record['field'] is not defined at all, not even null
}

我的目的是检查是否定义了$record['field'],甚至是null

但是,每次$record['field']都没有设置(例如注释掉$ record [' field'] = null;),它会给我这个错误:

Undefined index: field

如果有人会问,这是因为当NULL值变得有意义时,例如在SQL查询中为时间戳列分配NULL值会使MySQL使用当前时间戳更新它。

我能做到这一点吗?

1 个答案:

答案 0 :(得分:1)

由于您所拥有的不仅仅是变量而是关联数组,您可以使用array_key_exists()检查数组是否包含具有该名称的字段,无论其值如何。