The current behaviour of increment and decrement operators is not very intuitive:
// booleans $a = false; ++$a; // bool(false) $a = true; --$a; // bool(true) // null values $a = null; --$a; // null $a = null; ++$a; // int(1) // empty strings $a = ''; ++$a; // string(1) "1" // non-numeric strings $a = '12d9'; ++$a; // string(4) "12e0" ++$a; // float(13)
The proposal is:
str_inc() and str_dec().// booleans $a = false; ++$a; // int(1) + warning ++$a; // int(2) $a = true; --$a; // int(0) + warning --$a; // int(-1) // null values $a = null; --$a; // int(-1) + warning $a = null; ++$a; // int(1) + warning
// non-numeric strings $a = '12d9'; ++$a; // string(4) "12e0" + Notice: String increment is deprecated, use str_inc() instead in php shell code on line 1 ++$a; // float(13)
Additionally, it makes two new string functions available:
str_inc($str) - to perform the current string increments.str_dec($str) - the string decrement.
Incrementing null will now raise a warning; incrementing alphanumeric strings will raise a deprecation notice.
PHP 7
None.
The changes do not affect the following data types:
arrayintfloatobjectresourceYay or nay.
Coming soon ...
N/A
Keep this updated with features that were discussed on the mail lists.