How can small numbers be accurately calculated in PHP without losing precision?

Small numbers in PHP can lose precision when performing calculations due to the way floating-point numbers are represented internally. To accurately calculate small numbers without losing precision, you can use the BCMath functions in PHP, which provide arbitrary precision mathematics. By using BCMath functions like bcadd, bcsub, bcmul, and bcdiv, you can perform calculations on small numbers with precision.

$number1 = '0.1';
$number2 = '0.2';

$sum = bcadd($number1, $number2, 10);
$diff = bcsub($number2, $number1, 10);
$product = bcmul($number1, $number2, 10);
$quotient = bcdiv($number2, $number1, 10);

echo "Sum: $sum\n";
echo "Difference: $diff\n";
echo "Product: $product\n";
echo "Quotient: $quotient\n";