Testing if a value is 0 or null

#1

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';

0 Likes

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

#3

dang, thoughtit might be

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

#4

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

0 Likes

#5

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.

0 Likes

#6

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

0 Likes

#7

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.

0 Likes