区分完全未设置和空值?

Distinguish between not set at all AND 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 使用当前更新它时间戳。

有什么方法可以做到这一点?

由于您在这里拥有的不仅仅是一个变量,而是一个关联数组,因此您可以使用 array_key_exists() 检查该数组是否包含具有该名称的字段,而不管其值如何。