Skip to content

Instantly share code, notes, and snippets.

@cupracer
Created August 24, 2021 20:49
Show Gist options
  • Save cupracer/e3648851abaa72d1ab7dd4cb22feeabb to your computer and use it in GitHub Desktop.
Save cupracer/e3648851abaa72d1ab7dd4cb22feeabb to your computer and use it in GitHub Desktop.
Don't compare float values in PHP!
<?php
$a = 0.20;
// Test 1:
$b = 1 - 0.80; // 0.20
if ($a == $b) {
echo "true";
}else {
echo "false";
}
// Result: false
// Test 2:
$c = bcsub(1, 0.80, 2); // 0.20
if ($a == $c) {
echo "true";
}else {
echo "false";
}
// Result: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment