Decrement on type null has no effect, this will change in the next major version of PHP

Description

Pre or post increment on a null leaves the value unchanged. While PHP updates strings, integers, floats with the post- or pre-increment operators, null are immune.

Until PHP 8.3, it was silently done. In PHP 8.3, it raises a deprecation warning, and it will be removed in PHP 9.0.

Post increment, pre increment, as well as increment and decrement are affected by that warning. str_increment() and str_decrement() refuses to use null, due to type.

Example

<?php

$a = null;
$a--;
--$a;
// $a === null

?>

Solutions

  • Avoid initializing variables with null before decrementing them.

  • In case of doubt, check the type before the increment.

See Also