php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41690 Decrementing null is null
Submitted: 2007-06-14 14:31 UTC Modified: 2007-06-25 18:46 UTC
From: ian at ithomas dot name Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.3 OS: Gentoo
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: ian at ithomas dot name
New email:
PHP Version: OS:

 

 [2007-06-14 14:31 UTC] ian at ithomas dot name
Description:
------------
Incrementing null is 1.
Decrementing null is null!

I'm not sure which is the correct behaviour, but they should at least be consistent.


I saw this on PHP 4.4.4 but have since duplicated it on 5.2.3. It was a problem for me because I had a "for ($i = null; $i > 0; $i--)" which results in an infinite loop (but would work fine if $i++ was used instead).


Reproduce code:
---------------
<?php
echo '$i++: ';
$i = null;
var_dump($i);
$i++;
var_dump($i);

echo '<br> <br> <br> $i--: ';
$i = null;
var_dump($i);
$i--;
var_dump($i);

echo '<br> <br> <br> $i - 1: ';
$i = null;
var_dump($i);
$i = $i - 1;
var_dump($i);
?>

Expected result:
----------------
null-- should be 1, so script should output:

$i++: NULL int(1)


$i--: NULL int(-1)


$i - 1: NULL int(-1) 

Actual result:
--------------
null-- is null, script output is:


$i++: NULL int(1)


$i--: NULL NULL


$i - 1: NULL int(-1) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-14 15:39 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 [2007-06-14 15:46 UTC] ian at ithomas dot name
Thanks for your response. Since filing this I've found http://uk2.php.net/manual/en/language.operators.increment.php which states:

"Decrementing NULL values has no effect too, but incrementing them results in 1."

but with no further explanation. I assume it is because this behaviour is documented that you are saying that this is not a bug.


This behaviour is counter-intuitive, so unless there is a good reason for it (which should be mentioned in the documentation) it remains a bug. Documenting a bug doesn't fix it!
 [2007-06-16 12:54 UTC] ian at ithomas dot name
Sorry, forgot to reopen it. See comment above for why.
 [2007-06-25 18:46 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 08:01:29 2025 UTC