Google News
logo
PHP Program to Invalid calculation will return a NaN value
In PHP, performing invalid calculations, such as dividing a number by zero, will result in a special value called "Not a Number" or `NaN`. `NaN` is used to represent the result of an undefined or unrepresentable value.

Here's an example :
Program :
<?php
$number1 = 10;
$number2 = 0;

$result = $number1 / $number2;

if (is_nan($result)) {
    echo "The result is not a number.";
} else {
    echo "The result is: $result";
}
?>
In this example, the variables `$number1` and `$number2` are assigned the values `10` and `0`, respectively. The `$result` variable is assigned the value of `$number1` divided by `$number2`. Since dividing a number by zero is undefined, the result of the calculation is `NaN`.

The `is_nan()` function is used to check if the value of `$result` is `NaN`. If `$result` is `NaN`, the message "The result is not a number" will be printed. Otherwise, the message "The result is: " followed by the value of `$result` will be printed.

It's important to handle `NaN` values in your PHP code, as they can cause unexpected behavior and errors if not properly handled.