This is an old revision of the document!
PHP RFC: Path to Saner Increment/Decrement operators
- Version: 0.2
- Date: 2022-11-21
- Author: George Peter Banyard, girgias@php.net
- Status: Draft
- Target Version: PHP 8.3 and PHP 9.0
- Implementation: https://github.com/php/php-src/pull/
- First Published at: http://wiki.php.net/rfc/saner-inc-dec-operators
Introduction
PHP's increment and decrement operators can have some surprising behaviours when used with types other than int and float. Various previous attempts 1) 2) 3) have been made to improve the behaviour of these operators. But none have been implemented.
As a design principle, we will follow that the increment and decrement operators should behave like adding or subtracting 1 respectively.
Therefore, we will first look at the behaviour of arithmetic operators with various types, then detail the current behaviour of the increment and decrement operators, and finally propose various changes to fix the dependencies.
Behaviour of arithmetic operators
Arithmetic operators perform a numeric type juggling, which is described in the userland manual as:
In this context if either operand is a float (or not interpretable as an int), both operands are interpreted as floats, and the result will be a float. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted a TypeError is thrown.
The following types (other than int and float) are considered interpretable as int/float:
null
, as0
bool
, wherefalse
is interpreted as0
, andtrue
as1
string
, if it is numeric the string is converted to int/float and the standard behaviour is usedobject
if it implements ado_operation
handler that supports the operation, this is used. Otherwise, if it implements a customcast_object
handler which supports a numeric (but not an int or float) cast, the object is casted and the standard behaviour is used. This is limited to internal objects. One such example is the GMP class.
All other cases throw a TypeError
.
Note: the empty string has never been considered numeric. (see: https://3v4l.org/uvLbV)
Current behaviour of the increment and decrement operators
The current behaviour of these operators is rather complex and depends on which operator is used with which type. First, we will describe the common behaviour between both operators:
- the value is of type
int
orfloat
, the operation is performed - the value is of type
array
orresource
then aTypeError
is raised - the value is of type
bool
, no action is performed on the value - the value is of type
object
and it implements ado_operation
handler that supports the addition and/or subtraction operation, then the corresponding operation is performed. Otherwise, aTypeError
is raised. Examples of such internal objects areGMP
orFFI\CData
. - the value is of type
string
and is numeric, then a standard numeric type cast is performed, and theint
/float
behaviour is utilized.
For non-numeric string
s values and values of type null
the behaviour is different between the increment and decrement operators.
Current behaviour of the decrement operator with values of type null and non-numeric string
No action is performed, except if the value is the empty string, in which case the value is set to the integer -1
.
Current behaviour of the increment operator with values of type null and non-numeric string
If the value is of type null
, the value is cast to the integer 1
.
If the value is a non-numeric string
a PERL alphanumeric string increment is performed.
Note: this means that the behaviour around the empty string differs between both operators. Because for ++
a PERL increment is used, the result is the string “1”
. This behaviour is identical in all versions of PHP.
<?php $s1 = $s2 = ""; var_dump(++$s1, ++$s1, --$s2, --$s2); /* this results in string(1) "1" int(2) int(-1) int(-2) */
Proposal
The proposal is to create a path so that in the next major version of PHP the increment and decrement operators behave identically to adding/subtracting 1 respectively.
To achieve this, we propose the following changes to be made in the next minor version of PHP:
- Add support to increment/decrement objects that implement support for a
_IS_NUMBER
cast but do not implement ado_operation
handle - to emit
E_WARNING
s when the operators currently do not have any behaviour when they would if replace with a proper addition/subtraction (i.e. when the value is of typebool
andnull
for the decrement operator). - Deprecate using those operators with non-numeric strings.
In the next major version of PHP the following changes will take place:
- Values of type
bool
andnull
are first cast to integers - Non-numeric string values throw a
TypeError
Cost/Benefit
PHP currently has 6 main and 3 operation specific type juggling contexts. The main 6 are documented in the userland manual on the type juggling page and are as follows:
- Numeric
- String
- Logical
- Integral and string
- Comparative
- Function
The 3 operation specific one are:
- Increment/Decrement operators
- String offsets
- Array offsets
With the semantics proposed in this RFC the increment/decrement operators would be folded into the numeric type juggling context which reduces the semantic complexity of the language and possibly the engine/optimizer implementation in the next major version.
The drawback of this approach is the deprecation, and thus removal, of the PERL increment feature. However, as supporting string decrements has been rejected unanimously. It is only handling ASCII strings where the last byte is in the following ranges [0-9], [a-z], [A-Z], and doing nothing silently otherwise. We consider the value of reducing the semantic complexity of PHP higher than keeping support for this feature, which may be implemented more completely (such as Unicode support, and decrement like Raku) with more rigorous behaviour in userland.
Backward Incompatible Changes
Using the increment/decrement operators on the empty string.
The string increment feature.
The changes that introduce an E_WARNING
diagnostic do not technically break backwards compatibility, however they might be elevated to an exception via a user set error handler which may reveal some unintended usages.
Future Scope
One possible future scope is to add support to both arithmetic operations and the increment/decrement operators to support objects that only implement an int or float cast instead of a numeric cast.
Proposed PHP Version
Next minor version, i.e. PHP 8.3.0, and next major version, i.e. PHP 9.0.0.
Proposed Voting Choices
As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Voting started on 2023-XX-XX and will end on 2023-XX-XX.
Implementation
GitHub pull request: https://github.com/php/php-src/pull/XXXX
After the project is implemented, this section should contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature