If statement problems

Im using this to check if a variable is NULL, if it is, I want its value to be 150

$width = isset($row2['width']) ? $row2['width'] : '150';

but if I try it on a not null variable


you can see the width of the first row is 30, but when I run the code

echo '   <rect x="'.$beginning_x.'" y="'.(568 - ($row2["ending_slot"]*12)).'" width="'.$width.'" height="'.($row2['ending_slot']-$row2['beginning_slot'])*12 .'" class="device jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="'.$row2['device'].'" />';

I get

<rect x="25" y="378.4" width="150" height="9.6" class="device jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="M/C" />

is isset() not the best way to test for NULL?

NULL in this case does not mean null.
Null fetched from a mysql table, is the empty string.

The empty string (or null itself, for that matter) set to an array key, still means the array key is SET, so isset returns True.
Check for the empty string, or for empty() (but that has its own caveats. Like 0.)

1 Like

thats strange cause if I change it to

$width = empty($row2['width']) ? $row2['width'] : '150';

I get

Notice: Undefined index: width in C:\Users\lurtnowski\webserver\htdocs\ICE-v-4.6 -php\san-diego\show_racks_inc.php on line 123

oh,was missing width from the select query.
Thanks

$width = !empty($row2['width']) ? $row2['width'] : '150';