How can the round() function be effectively used to handle numerical precision in PHP calculations?
When performing calculations in PHP, especially with floating-point numbers, there can be issues with numerical precision leading to unexpected results. To handle this, the round() function can be used to round the result to a specified number of decimal places, reducing the impact of precision errors.
$number1 = 10.555;
$number2 = 20.333;
$result = $number1 + $number2;
// Round the result to 2 decimal places
$roundedResult = round($result, 2);
echo $roundedResult; // Output: 30.89