$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
}
My intention is to check if $record['field']
is defined at all, even null
.
However every time $record['field']
is not set at all (e.g. comment out $record['field'] = null;), it gives me this error:
Undefined index: field
In case anyone would ask, this is for when NULL values become meaningful, such as in a SQL query assigning a NULL value to a timestamp column would make MySQL update it with the current timestamp.
Any way I can do this?
Since what you have here is not just a variable but an associative array, you can use
array_key_exists()
to check that the array contains a field with that name, regardless of its value.