Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP zero float returning true

I have a field in my mySQL db table of type decimal (15,4)

I've never experienced this before but if the value is 0.0000 my PHP if statement is returning true!

As a sanity check I even did:

if(0.0000) echo "Hello World";

And sure enough, Hello World echo'd out. What the hell is going on? Anybody got any ideas?

like image 833
Andy Avatar asked Feb 20 '23 08:02

Andy


2 Answers

If it's a float value coming out from DB it will be treated like a string, not like a numeric value. You can try something this:

if(floatval($value) > 0) { ... }

Where $value contains the value from DB.

like image 118
ulentini Avatar answered Feb 22 '23 23:02

ulentini


I think the problem is you got a string "0.0000" from db but not 0.0000.

Try again with:

if ((int)$your_value) echo "Hello World";
like image 25
xdazz Avatar answered Feb 22 '23 21:02

xdazz