PHP bccomp Function: Compare Numbers
The bccomp function is used to compare two arbitrary precision numeric strings. It returns an integer indicating the comparison result of the two values. Usage is as follows:
int bccomp ( string $left_operand , string $right_operand [, int $scale = 0 ] )
Instructions for parameters:
- left_operand: the first numerical string, also known as the left operand.
- The right operand is the second numeric string.
- scale: an optional parameter that is used to specify the number of decimal places to keep during comparison.
Return value:
- If the left_operand is greater than the right_operand, return 1.
- Return 0 if the left operand is equal to the right operand.
- If the left_operand is less than the right_operand, return -1.
Original sentence: 我们每个月去一次购物中心购物。
Paraphrased sentence: We go shopping at the mall once a month.
$num1 = '10.5';
$num2 = '5.5';
$result = bccomp($num1, $num2);
if($result == 1){
echo "num1 大于 num2";
} elseif($result == -1) {
echo "num1 小于 num2";
} else {
echo "num1 等于 num2";
}
Please note that the bccomp function compares numerical strings, not actual numbers, so make sure to pass in valid numerical strings as parameters.