parent root
PHP: DivisionByZeroError - Manual
PHP 7.2.23 Release Announcement

DivisionByZeroError

(PHP 7)

Introduction

DivisionByZeroError is thrown when an attempt is made to divide a number by zero.

Class synopsis

DivisionByZeroError extends ArithmeticError {
/* Inherited properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Inherited methods */
final public Error::getMessage ( void ) : string
final public Error::getPrevious ( void ) : Throwable
final public Error::getCode ( void ) : mixed
final public Error::getFile ( void ) : string
final public Error::getLine ( void ) : int
final public Error::getTrace ( void ) : array
final public Error::getTraceAsString ( void ) : string
public Error::__toString ( void ) : string
final private Error::__clone ( void ) : void
}
add a noteadd a note

User Contributed Notes 3 notes

up
11
salsi at icosaedro dot it
3 years ago
Note that on division by zero 1/0 and module by zero 1%0 an E_WARNING is triggered first (probably for backward compatibility with PHP5), then the DivisionByZeroError exception is thrown next.

The result is, for example, that if you set the maximum level of error detection with error_level(-1) and you also map errors to exception, say ErrorException, then on division by zero only this latter exception is thrown reporting "Division by zero". The result is that a code like this:

<?php
// Set a safe environment:
error_reporting(-1);

// Maps errors to ErrorException.
function my_error_handler($errno, $message)
{ throw new
ErrorException($message); }

try {
    echo
1/0;
}
catch(
ErrorException $e){
    echo
"got $e";
}
?>

allows to detect such error in the same way under PHP5 and PHP7, although the DivisionByZeroError exception is masked off by ErrorException.
up
0
Alex
4 months ago
This error is thrown only for integer division - this is when using "intdiv" function or "%" operator. In all cases you will get an E_WARNING when dividing by zero.
up
-17
Manjunath
3 years ago
<?php
class MathOperation extends Error
{
    protected
$n = 10;

   
// Try to get the Division by Zero error object and display as Exception
   
public function doArithmeticOperation(): string
   
{
        try {
           
$value = $this->n % 0;
        } catch (
DivisionByZeroError $e) {
            return
$e->getMessage();
        }
    }
}

$mathOperationObj = new MathOperation();
echo
$mathOperationObj->doArithmeticOperation();
To Top
parent root