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

Description

Post increment on a boolean leaves the value unchanged. While PHP updates strings, integers, floats with the post-increment operator, booleans 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 boolean, due to type.

Example

<?php

$a = true;
$a++;
// $a = true

$b = false;
--$b;
// $b = false

?>

Solutions

  • Avoid initializing variables with booleans before incrementing them.

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

See Also