เพื่อเปรียบเทียบค่า float ใน PHP โค้ดมีดังนี้ −
ตัวอย่าง
<?php
$a = 2.5;
$b = 3.5;
echo $a;
echo "\n$b";
if($a == $b) {
echo "\nBoth the values are equal!";
} else {
echo "\nBoth the values aren't equal";
}
?>ผลลัพธ์
จะผลลัพธ์ดังนี้ −
2.5 3.5 Both the values aren't equal
ตัวอย่าง
มาดูตัวอย่างอีกอย่าง −
<?php
$a = 1.967;
$b = 1.969;
echo $a;
echo "\n$b";
if((round($a, 2)) == (round($b, 2))) {
echo "\nBoth the values are equal!";
} else {
echo "\nBoth the values aren't equal";
}
?>ผลลัพธ์
จะผลลัพธ์ดังนี้ −
1.967 1.969 Both the values are equal!