atomCode atomCode - 7 months ago 21
PHP Question

Is it "Bad" practice to multiply $val * 1 to convert null to zero

I'm using PHP 5.5 to retrieve a numeric value from a numeric postgresql database table field. It could come from the DB as null or as an actual number, either way I store it in a variable. As some of us may or may not know null * 1 = 0, which is something I like to do but I'm concerned about whether this is wrong or whether it is bad practice to do to convert potential null values into zeros without actually having to check the value. See below:

$fieldVal *= 1; //assume at this point this holds value returned by DB

So if the db returned null then $fieldVal will be turned into zero and any non-null values will remain unchanged. Your insights are much appreciated.


Change your query to include COALESCE and resolve the null cases

SELECT fieldNane



BTW: Yes is bad practice introduce those kind of hacks.