2 つの同一の float 値を一致させるのに問題があります。スクリプトの背後にある基本は、収益の API をクエリし、MySQL データベースにレコードが存在しない場合は作成し、存在する場合は変更を探し、レコードを更新またはスキップすることです。
例として、1 つのレコードを見てみましょう。売上高は 51.02 で、これは強制的に float になり、データベースに保存されます。データベースの列の型は float です。
次に API を再度クエリすると、そのレコードは既に存在するため、収益額の変化を確認します。収益の数値は再び 51.02 (変化なし) であり、フロートのままです。次に、格納されている図 (これも浮動小数点数) を取得しますが、値は同じです。ただし、フロートが一致しないため、レコードを更新する必要があると PHP が通知しています。どうしてこれなの?!
調べるコード:
// 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)"
誰にもアイデアはありますか?float 比較は他の比較とは異なりますか?
前もって感謝します。