What are some best practices for handling mathematical calculations, such as logarithms, in PHP scripts to avoid fatal errors and ensure accurate results?

When handling mathematical calculations like logarithms in PHP scripts, it is important to handle potential errors such as division by zero or taking the logarithm of a negative number. To avoid fatal errors and ensure accurate results, you can use conditional statements to check for these edge cases before performing the calculation.

// Check if the input is valid for logarithm calculation
if ($input > 0) {
    $result = log($input);
    echo "The logarithm of $input is: $result";
} else {
    echo "Invalid input for logarithm calculation";
}