On Sat, Feb 15, 2020 at 6:44 PM Rowan Tommins <rowan.coll...@gmail.com> wrote:
> 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. > Principally in favor of this change, I do think that ++ and -- should behave consistently if nothing else. We might want to consider giving the same treatment to false/true as well, which should be interpreted as 0/1. That is $foo++ / $foo-- should behave the same ways as $foo+=1, $foo-=1 for null, true, false. It seems odd to single out only "null" here. Additionally I would suggest a notice when trying to increment arrays, resources and objects, rather than just silently doing nothing. As long as it's just a notice, this should have minimal BC implications. Nikita