Show price if not null or 0.00

hi all

the price field is set as float

In my database there are some prices fields which have either “0.00” or “NULL” as value

i wanted to display price if the value is not “0.00” or not “NULL”

if( ($row['price'] != "0.00") || ($row['price'] != "NULL") ){

But the price is being shown even if its “0.00”.

Is my code correct or not ?


AND (&&), not OR (||).

if the price is 0.00:
if(false OR true) = true.
If the price is null
if(true OR false) = true.

thanks starlion


To be more precise, the way to logically construct the description of what you want is:

If the price is Neither “0.00” Nor Null, show price.
“Neither A Nor B” translates to logic as “Not A AND Not B”

“Neither A Nor B” translates to logic as “Not ( A OR B )”


An SQL SELECT statement returns these rows, would it be better if it did not return those rows whose price was NULL or “0.00”?

vinpkl, also remember that the database value NULL when retrieved by PHP becomes the PHP NULL value and not the “NULL” string. Therefore your condition as you presented will not work:

$row['price'] != "NULL"

Instead, you should write it like this:

$row['price'] !== NULL

This way you will strictly exlude NULL values.

But in PHP you can achieve your goal in a much simpler syntax because of default type casting of values:

if($row['price'] != 0) {

This will display the price if it is not “0.00” or if it is not NULL, because in PHP using weak comparison (!= and ==) NULL equals 0, and string “0.00” equals 0. Only when you use strong comparison (!== or ===) then NULL does not equal 0 and string “0.00” does not equal 0.