Allow null variables to be decremented
| From: | Rowan Tommins | Date: | Sat, 15 Feb 2020 17:44:05 +0000 |
| Subject: | Allow null variables to be decremented | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-108602@lists.php.net to get a copy of this message | ||
Hi all,
There is currently an odd inconsistency when using the decrement operator on a null variable:
$a = null; $a=$a+1; // int(1)
$a = null; $a+=1; // int(1)
$a = null; ++$a; // int(1)
$a = null; $a=$a-1; // int(-1)
$a = null; $a-=1; // int(-1)
$a = null; --$a; // null
I would like to propose changing this behaviour for PHP 8, so that --$a would give int(-1), as I believe it is simply a long-standing bug.
This has been raised as a bug at least three times [1][2][3] but closed as documented behaviour / too much of a BC break. It is documented in the manual, but with no explanation of why it should work that way. [4]
I would be interested in any explanations of why it might be intended behaviour, or ways in which people might be relying on the current behaviour.
A proposal to change the behaviour was included in a wider RFC about standardising increment and decrement behaviour, but it never got beyond draft status. [5] I would prefer not to reopen that wider debate, but focus on this single issue.
As far as I can see, the change would be to add a "case IS_NULL" branch to decrement_function in zend_operators.c to match the one in increment_function. [6]
I will happily write up an RFC to formalise this, but wanted to gather people's immediate thoughts first.
Links:
[1] https://bugs.php.net/bug.php?id=20548
[2] https://bugs.php.net/bug.php?id=25674
[3] https://bugs.php.net/bug.php?id=41690
[4] https://www.php.net/manual/en/language.operators.increment.php
[5] https://wiki.php.net/rfc/normalize_inc_dec
[6] https://php-lxr.adamharvey.name/source/xref/master/Zend/zend_operators.c#2359
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]