|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20365 the modulo operator % doesn't work with negative integers!
Submitted: 2002-11-11 09:40 UTC Modified: 2002-11-11 11:51 UTC
From: Ernest at Beinrohr dot sk Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.2.2 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: Ernest at Beinrohr dot sk
New email:
PHP Version: OS:


 [2002-11-11 09:40 UTC] Ernest at Beinrohr dot sk
PHP's modulo operator cannot work with negative numbers. The positive ones work fine, but modulo is defined also on negatives ;-)

ex:  10%3  = 1  [php gives 1]
     -4%10 = 6  [php gives NULL] 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-11 09:51 UTC]
Works fine (it should output -4):

[derick@kossu derick]$ php
$a = -4 % 10;

Please provide a short script that reproduces this problem.

 [2002-11-11 09:53 UTC]
Please try using this CVS snapshot:
For Windows:

works fine here too..(with 4.3.0-dev) so try the snapshot.

 [2002-11-11 11:09 UTC] Ernest at Beinrohr dot sk
Yes, you are right, the output is not NULL, it's -4. 

BUT, it should be 6. 

PS: but bc gives -4 too, now I do not understand. I suppose, BC is wrong too. It should be 6. [ -4=6 (mod 10) ]
 [2002-11-11 11:51 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

No, it should be -4. If even BC says it who are we to go into that?
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 23 07:01:29 2024 UTC