1

How to make auto convert a decimal data type into number not a string in php?

I changed data field into decimal(4,2) from double. I have many checks like

if ($row->field)

And $row->field === 0.00. When it was double it's ok. But when I get decimal in php it's get string type and '0.00' it's TRUE.

"Add (float) before $row->field" are not my way, because I change many field and too many places where I use it.

I want find a better way, than change too many lines of code. Maybe some extensions or something else.

2
  • The example if($row->field) is always better coded as if($row->field != 0) or if($row->field > 0) if those are what is meant. Commented Nov 2, 2013 at 5:24
  • I know, but code was written and now I need to refactor this moment. Commented Nov 2, 2013 at 5:37

1 Answer 1

1

Why not return a boolean expression from MySQL itself as part of your query?

SELECT decimal_column <> 0.00 as bool_expression FROM table;

Then test the expression like: if($row->bool_expression)

Sign up to request clarification or add additional context in comments.

3 Comments

Because I want find a better way, than change too many lines of code. Maybe some extensions or smth else.
The mechanism presented in this answer will work if you test only (and use the old column name), but not if the value itself is used. Either way, the data layer change seems will necessitate refactoring.
In my way, It using with active record and easier way to change code like if($row->field != 0) than change sql queries. However thanks for answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.