Is it safe to use decimal type values coming from MySQL as strings in PHP for arithmetic calculations?

Viewed 32

There's a huge concern regarding decimal to float conversion in PHP, because of precision issues.

I wonder why nobody talks about using the decimals as strings and do all the calcs in that way. It seems that PHP works pretty good that way:

"1.123456" / "1.123456"
"1.123456" * "1.123456"
"1.123456" + "1.123456"
"1.123456" - "1.123456"

All those work perfectly fine.

So, what are the cons?

By the way, Laravel's Collection has a method "sum", which might rely on strings to do the operation.

For instance:

// It does the math on MySQL
$orders = OrderModel::sum('total');

// It does the math on PHP, not on MySQL
$orders = OrderModel::get()->sum('total');

Laravel doesn't warn about this. And it seems nobody either. So I might assume is totally reliable.

0 Answers
Related