Testing if a value is 0 or null

how can I test to see if value is 0 or null

$beginning_x = isset($row['beginning_x']) || $row['beginning_x'] == 0 ? $row['beginning_x'] : '25';

$beginning_x = !empty($row['beginning_x']) ? $row['beginning_x'] : '25';
1 Like

dang, thoughtit might be

$beginning_x = (($row['beginning_x'] != NULL) || $row['beginning_x'] != 0) ? $row['beginning_x'] : '25';

That would work too. It’s even more concise than mine, since emtpy string is considered empty too…

Also it’s worth noting that an unset key is not the same as a set key with value of null.

EDIT: Okay, technically i meant ‘never set’ rather than unset. An unset key is null.

thats wierd, cause it works using your code, but when I use

$beginning_x = (($row['beginning_x'] != NULL) || ($row['beginning_x'] != 0)) ? $row['beginning_x'] : '25';

I get
http://lukesspot.com/indus_links/update_devices.php?id=9
But heres the database

You should use == instead of != :

$beginning_x = (($row['beginning_x'] === NULL) || ($row['beginning_x'] === 0)) ? $row['beginning_x'] : '25';

I’d maybe even go for:

$beginning_x = (int) $row['beginning_x'] === 0 ? '25' : $row['beginning_x'];

This works because (int) null is 0 and seems to convey most intent of all proposed solutions so far.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.