# RFC: Normalizing increment and decrement operators

- Status: Inactive
- Authors: Tjerk Meesters (datibbaw)
- Date: 2013-12-19
- Version: 0.2
- Original: https://wiki.php.net/rfc/normalize_inc_dec
- Page: https://externals.io/rfc/normalize_inc_dec
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [\[rfc\] normalize increment and decrement operators](https://externals.io/message/72621.md) — started on 2014-02-15, 2 messages
- [Allow null variables to be decremented](https://externals.io/message/108602.md) — started on 2020-02-15, 12 messages

---

## Introduction

The current behaviour of increment and decrement operators is not very intuitive:

```php
// 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)
```

## Proposal

The proposal is:

1. always treat boolean and null types as an integer, but raise a warning.
2. deprecate alphanumeric increment and introduce `str_inc()` and `str_dec()`.

### Operation on bool / null

```php
// 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
```

### Operation on alphanumeric strings

```php
// 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.

## Backward Incompatible Changes

Incrementing `null` will now raise a warning; incrementing alphanumeric strings will raise a deprecation notice.

## Proposed PHP Version(s)

**PHP 7**

## Open Issues

None.

## Unaffected PHP Functionality

The changes do not affect the following data types:

- `array`
- `int`
- `float`
- `object`
- `resource`

## Proposed Voting Choices

Yay or nay.

## Patches and Tests

Coming soon ...

## Implementation

N/A

## References

[Pull Request](https://github.com/php/php-src/pull/547)

The competing proposal:

[Alphanumeric Decrement](https://externals.io/rfc/alpanumeric_decrement)

## Rejected Features

Keep this updated with features that were discussed on the mail lists.
