diff --git a/language/operators/increment.xml b/language/operators/increment.xml index af0e685b9821..c31a783203b5 100644 --- a/language/operators/increment.xml +++ b/language/operators/increment.xml @@ -2,17 +2,9 @@ Incrementing/Decrementing Operators - PHP supports C-style pre- and post-increment and decrement - operators. + PHP supports pre- and post-increment and decrement operators. + Those unary operators allow to increment or decrement the value by one. - - - The increment/decrement operators only affect numbers and strings. - Arrays, objects, booleans and resources are not affected. - Decrementing &null; values has no effect too, but incrementing them - results in 1. - - Increment/decrement Operators @@ -47,48 +39,110 @@
+ Here's a simple example script: Postincrement"; +echo 'Post-increment:', PHP_EOL; $a = 5; -echo "Should be 5: " . $a++ . "
\n"; -echo "Should be 6: " . $a . "
\n"; +var_dump($a++); +var_dump($a); -echo "

Preincrement

"; +echo 'Pre-increment:', PHP_EOL; $a = 5; -echo "Should be 6: " . ++$a . "
\n"; -echo "Should be 6: " . $a . "
\n"; +var_dump(++$a); +var_dump($a); -echo "

Postdecrement

"; +echo 'Post-decrement:', PHP_EOL; $a = 5; -echo "Should be 5: " . $a-- . "
\n"; -echo "Should be 4: " . $a . "
\n"; +var_dump($a--); +var_dump($a); -echo "

Predecrement

"; +echo 'Pre-decrement:', PHP_EOL; $a = 5; -echo "Should be 4: " . --$a . "
\n"; -echo "Should be 4: " . $a . "
\n"; +var_dump(--$a); +var_dump($a); ?> ]]>
+ &example.outputs; + + +
+ + + + The increment and decrement operators have no effect on values + of type bool. + A E_WARNING is emitted as of PHP 8.3.0, + because this will implicitly cast the value to int in the future. + + + The decrement operator has no effect on values + of type null. + A E_WARNING is emitted as of PHP 8.3.0, + because this will implicitly cast the value to int in the future. + + + The decrement operator has no effect on non- + numeric string. + A E_WARNING is emitted as of PHP 8.3.0, + because a TypeError will be thrown in the future. + + + The decrement operator has no effect on non- + numeric string. + A E_WARNING is emitted as of PHP 8.3.0, + because a TypeError will be thrown in the future. + + + + + Internal objects that support overloading addition and/or subtraction + can also be incremented and/or decremented. + One such internal object is GMP. + +
- - PHP follows Perl's convention when dealing with arithmetic operations - on character variables and not C's. For example, in PHP and Perl - $a = 'Z'; $a++; turns $a into 'AA', while in C - a = 'Z'; a++; turns a into '[' - (ASCII value of 'Z' is 90, ASCII value of '[' is 91). - Note that character variables can be incremented but not decremented and - even so only plain ASCII letters and digits (a-z, A-Z and 0-9) are supported. - Incrementing/decrementing other character variables has no effect, the - original string is unchanged. + + + PERL string increment feature + + + This feature is soft-deprecated as of PHP 8.3.0. + The str_increment function should be used instead. + + + + + It is possible to increment a non- + numeric string + in PHP. The string must be an alphanumerical ASCII string. + Which increments letters up to the next letter, when reaching the letter + Z the increment is carried to the value on the left. + For example, $a = 'Z'; $a++; turns $a + into 'AA'. + + - Arithmetic Operations on Character Variables + PERL string increment example - - - Incrementing or decrementing booleans has no effect. - + + + + If the alphanumerical string can be interpreted as a + numeric string + it will be cast to an int or float. + This is particularly an issue with strings that look like a floating point + numbers written in exponential notation. + The str_increment function does not suffer from + these implicit type cast. + + + Alphanumerical string converted to float + + +]]> + + &example.outputs; + + + + + This is because the value "5e0" is interpreted + as a float and cast to the value 5.0 + before being incremented. + + + +