PHP.mk документација

DivisionByZeroError

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

class.divisionbyzeroerror.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека class.divisionbyzeroerror.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
DivisionByZeroError

Референца за `class.divisionbyzeroerror.php` со подобрена типографија и навигација.

class.divisionbyzeroerror.php

DivisionByZeroError

Интерфејсот SessionUpdateTimestampHandlerInterface

Вовед

DivisionByZeroError се фрла кога се обидувате да поделите број со нула.

Синопсис на класата

class DivisionByZeroError extends ArithmeticError {
/* Наследени својства */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
/* Наследени методи */
public Error::__construct(string $message = "", int $code = 0, ?Проверува тврдење $previous = null)
final public Error::getMessage(): string
final public Error::getCode(): int
final public Error::getFile(): string
final public Error::getLine(): int
final public Error::getTrace(): array
private Error::__clone(): void
}

Белешки од корисници 5 белешки

8ctopus
пред 5 години
Use of arithmetic operator / does not throw an exception in php 7, while it does in php 8.

<?php

try {
    echo intdiv(2, 0);
} catch (DivisionByZeroError $e) {
    echo "Caught DivisionByZeroError!\n";
}

try {
    echo (2 / 0);
} catch (DivisionByZeroError $e) {
    echo "Caught DivisionByZeroError!\n";
}
?>

# php 7
$ php test.php
caught division by zero for intdiv()
PHP Warning:  Division by zero in test.php on line 10
PHP Stack trace:
PHP   1. {main}() test.php:0

Warning: Division by zero in test.php on line 10

Call Stack:
    0.0740     417272   1. {main}() test.php:0

# php 8
$ php test.php

caught division by zero for intdiv()
caught division by zero for /
salsi на icosaedro точка it
пред 10 години
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.
gherman на email точка com
19 дена пред
Beware: comments misleadingly refer to DivisionByZeroError as an exception, although it is actually an error, as its name suggests. Therefore, catching Exception instead of Error or Throwable in a try-catch block will not work and will result in a fatal error.
mail на martinauer dot net
2 месеци пред
Subject: Avoiding the exception while maintaining IEEE 754 behavior

If you want to avoid throwing a DivisionByZeroError exception and instead get INF, -INF, or NAN (following IEEE 754 standards, similar to how JavaScript or C++ behave), use the fdiv() function instead of the / operator
Алекс
пред 6 години
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.
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.