php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71306 Remove Division by zero warning from "/" operator
Submitted: 2016-01-07 23:27 UTC Modified: 2020-01-09 11:50 UTC
Votes:6
Avg. Score:3.3 ± 1.4
Reproduced:3 of 4 (75.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: hungry dot rahly at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: irrelevant OS: irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hungry dot rahly at gmail dot com
New email:
PHP Version: OS:

 

 [2016-01-07 23:27 UTC] hungry dot rahly at gmail dot com
Description:
------------
It doesn't seem to throw an exception for an ACTUAL division of 0 ($var1 / $var2) and still presents as a warning

Test script:
---------------
<?php

echo PHP_VERSION."\n";
$x = 5;
$y = 0;

try {
  print "here1\n";
  $z = $x % $y;
  print "here2 - $z\n";
} catch(Error $e) {
  print "here ERROR 1 ".get_class($e)."\n";
}

try {
  print "here3\n";
  $z = $x / $y;
  print "here4 - $z\n";
} catch(Error $e) {
  print "here ERROR 2 ".get_class($e)."\n";
}

try {
  print "here5\n";
  $x = intdiv((int)$x, $y);
  print "here6 - $z\n";
} catch(Error $e) {
  print "here ERROR 3 ".get_class($e). "\n";
}


Expected result:
----------------
7.0.2
here1
here ERROR 1 DivisionByZeroError
here3
here ERROR 2 DivisionByZeroError
here5
here ERROR 3 DivisionByZeroError


Actual result:
--------------
7.0.2
here1
here ERROR 1 DivisionByZeroError
here3
PHP Warning:  Division by zero in test.php on line 17
here4 - INF
here5
here ERROR 3 DivisionByZeroError


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-08 00:43 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2016-01-08 00:43 UTC] bwoebi@php.net
This indeed is expected behavior; the only possible thing eventually subject to change in future is whether the warning will be removed.

But a DivisionByZeroError is definitely not suitable here as divisions by zero are handled like floating point divisions. (Yielding ±INF or NaN)
Just for integer divisions (intdiv) and modulo, there's no valid result, hence an exception is then thrown (instead of false like before).
 [2016-01-08 00:49 UTC] hungry dot rahly at gmail dot com
Then a removal of the warning would be best, because with the warning, you are saying there is a problem, when in fact, there isn't.
 [2016-01-09 00:24 UTC] yohgaki@php.net
-Summary: DivisionByZeroError Not Throwing +Summary: Remove Division by zero warning from "/" operator -Status: Not a bug +Status: Re-Opened -Type: Bug +Type: Feature/Change Request -Package: Unknown/Other Function +Package: Scripting Engine problem -Operating System: Fedora 22/23/24 +Operating System: irrelevant -PHP Version: 7.0.2 +PHP Version: irrelevant
 [2016-01-09 00:24 UTC] yohgaki@php.net
Make this a change request.
 [2016-03-10 13:08 UTC] axiac dot ro at gmail dot com
It would be great to have this behaviour (who throws the exception and who doesn't) explained in the documentation of the DivisionByZeroError exception.

I submitted a patch on the documentation site that adds this information.
 [2016-03-22 20:14 UTC] ajf@php.net
Interestingly fmod() doesn't produce a warning here, so if we got rid of the warning for `/` then we'd bring the two into alignment.
 [2020-01-09 11:50 UTC] nikic@php.net
-Status: Re-Opened +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-01-09 11:50 UTC] nikic@php.net
This has been changed to throw DivisionByZeroError in PHP 8. fdiv() is available to perform a silent division. See https://wiki.php.net/rfc/engine_warnings.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 20:01:28 2024 UTC