From 13d158a7e50fce3c61e8928b53716f1efb588dd9 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Thu, 5 Oct 2023 02:50:59 +0100 Subject: [PATCH 1/3] PHP 8.3: Describe increment/decrement operator changes --- language/operators/increment.xml | 167 ++++++++++++++++++++++++------- 1 file changed, 130 insertions(+), 37 deletions(-) diff --git a/language/operators/increment.xml b/language/operators/increment.xml index af0e685b9821..a53be729ce34 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,116 @@
+ + + + 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. + + + 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; + + +
- - 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. + + + +
From ce11e428fcef3006eb54f62d0e5dfce3cd37684a Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Sun, 8 Oct 2023 17:44:52 +0100 Subject: [PATCH 2/3] Nit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Düsterhus --- language/operators/increment.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/operators/increment.xml b/language/operators/increment.xml index a53be729ce34..356acde23d6b 100644 --- a/language/operators/increment.xml +++ b/language/operators/increment.xml @@ -44,7 +44,7 @@ The increment and decrement operators have no effect on values of type bool. - A E_WARNING is emitted, as of PHP 8.3.0, + A E_WARNING is emitted as of PHP 8.3.0, because this will implicitly cast the value to int in the future. From 85488fa68f6b8d7081e4c943e81ae1f91076c8d6 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Sun, 8 Oct 2023 17:46:49 +0100 Subject: [PATCH 3/3] review --- language/operators/increment.xml | 74 +++++++++++++++----------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/language/operators/increment.xml b/language/operators/increment.xml index 356acde23d6b..c31a783203b5 100644 --- a/language/operators/increment.xml +++ b/language/operators/increment.xml @@ -40,46 +40,6 @@ - - - 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. - - - Here's a simple example script: @@ -126,6 +86,40 @@ int(4) ]]> + + + + 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. + +