Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
fdiv() function in PHP 8
In PHP 8, the fdiv() function performs floating−point division according to the IEEE 754 standard. Unlike regular division, fdiv() allows division by zero without throwing errors, instead returning special IEEE 754 values.
Syntax
fdiv(float $num1, float $num2): float
Parameters
$num1 − The dividend (number to be divided)
$num2 − The divisor (number to divide by)
Return Value
The function returns one of the following values ?
INF (Infinity) − When a positive number is divided by zero
-INF (Negative Infinity) − When a negative number is divided by zero
NAN (Not a Number) − When zero is divided by zero or infinity is divided by infinity
Floating−point result − For normal division operations
Example 1: Basic Division
Here's a simple example demonstrating normal floating−point division ?
<?php echo fdiv(15, 4) . "<br>"; echo fdiv(20, 3) . "<br>"; echo fdiv(100, 8); ?>
3.75 6.6666666666667 12.5
Example 2: Division by Zero
This example shows how fdiv() handles division by zero cases ?
<?php echo fdiv(10, 0) . "<br>"; // Positive number / 0 echo fdiv(-10, 0) . "<br>"; // Negative number / 0 echo fdiv(0, 0); // Zero / 0 ?>
INF -INF NAN
Comparison with Regular Division
| Operation | Regular Division (/) | fdiv() Function |
|---|---|---|
| 10 / 3 | 3.3333... | 3.3333... |
| 10 / 0 | Fatal Error | INF |
| -10 / 0 | Fatal Error | -INF |
| 0 / 0 | Fatal Error | NAN |
Conclusion
The fdiv() function provides safe IEEE 754−compliant division that gracefully handles division by zero scenarios. It's particularly useful in mathematical calculations where you need to continue processing even when encountering division by zero.
