This problem drives me crazy. I have some other solution for it, but I would like top understand what is going on here, why php behaves like this in this case.
I have two MySql tables:
Packs:
+---------+-------------+
| Field | Type |
+---------+-------------+
| id | int(11) |
| price | double(7,3) |
| : | : |
+---------+-------------+
Contracts:
+-------------+---------------+
| Field | Type |
+-------------+---------------+
| id | int(11) |
| final_price | decimal(20,3) |
| : | : |
+-------------+---------------+
I retrieve SUM(price)
and SUM(final_price)
into two php variables ($totalPrice
and $totalFinalPrice
), then I need to compare them.
When I do:
var_dump(
gettype($totalPrice),
$totalPrice,
gettype($totalFinalPrice),
$totalFinalPrice,
$totalPrice == $totalFinalPrice);
I get:
string(6) "double"
float(39.669)
string(6) "string"
string(6) "39.669"
bool(false)
I also tried to cast both to float or to doubleval, but again, although var_dump
returns exactly the same value, the comparison returns false!
I saw this answer, but I doubt this is the reason in this case, since the values in the database in finite and equal.
========= UPDATE =======
It is impossible to reproduce this problem assigning directly these values into variables. Only after retrieving them from DB.
I use eloquent, here is the code:
$totalPrice = DB::table('Packs')
->selectRaw('SUM(price) AS total_price')
->whereIn('id_packs', [3371,3372,3373,3374])
->first()->total_price;
$totalFinalPrice = DB::table('Contracts')
->selectRaw('SUM(final_price) AS total_final_price')
->whereIn('id_contracts', [204028,204029,204030,204031])
->first()->total_final_price;
$totalPriceDouble = (double) $totalPrice;
$totalFinalPriceDouble = (double) $totalFinalPrice;
dd($totalPrice == $totalFinalPrice, $totalPriceDouble == $totalFinalPriceDouble);
which returns:
^ false
^ false
====== Update (2) =======
I tried to reproduce the problem with PHPize Online, here is the demo:
https://phpize.online/s/on
I don't know why in my local machine the var_dump
does not show the whole number, anyway this is what I see in the demo:
MySql:
+==========+========+
| id_packs | price |
+==========+========+
| 3371 | 27.260 |
| 3372 | 0.000 |
| 3373 | 0.000 |
| 3374 | 12.409 |
+----------+--------+
+==============+============+
| id_contracts | FinalPrice |
+==============+============+
| 204028 | 27.260 |
| 204029 | 0.000 |
| 204030 | 0.000 |
| 204031 | 12.409 |
+--------------+------------+
PHP:
After executing queries:
> float(39.669000000000004)
> string(6) "39.669"
After casting into double:
> float(39.669000000000004)
> float(39.669)