I am having trouble getting two identical float values to match. The basis behind the script is that I query an API for revenue and if the record doesn't exist in my MySQL database, create it, but if it does exist, look for changes and then either update or skip the record.
Let's take one record for example. The revenue figure is 51.02 and this is forced into being a float and is then saved in the database. The database column type is float.
When I then query the API a second time, that record already exists so I now check for changes in the revenue figure. The revenue figure is again 51.02 (no change) and continues to be a float. I'll then pull the stored figure (also a float) and the value is the same. However, PHP is telling me that the floats do not match so the record should be updated. Why is this?!
The code to examine:
// The placeholder to notify the script that the record should be updated
// Default is false. The record does not need updating.
$update_record = false;
// This is the database record
// I'm reinforcing the fact it needs to be a float
// The second line isn't actually needed
$record = $query_for_revenue_item->row ();
$record->basket_value = (float) $record->basket_value; // 51.02
// The API query data is stored in $transaction
// Again, this line isn't actually needed but reinforces the float
$transaction['basket'] = (float) $transaction['basket']; // 51.02
// Check for changes in revenue
// At this stage both floats are the same (51.02)
if ($record->basket_value != $transaction['basket']) {
$update_record = true;
}
// If I output the result
var_dump ($update_record) // returns boolean(true)
// The full record log (output below)
echo "Change data from \""; var_dump ($record->basket_value); echo "\" to \""; var_dump ($transaction['basket']); echo "\"";
// The output
Change data from "float(51.02)" to "float(51.02)"
Anyone have any ideas? Are float comparisons different to others?
Thanks in advance.