On 18/02/2022 07:48, Robert Landers wrote:
Just out of curiosity, why is the 3rd case being included here? Is it just
because it's currently a warning > When I first taught PHP in 2011, I was told
> post-increment/decrement was sugar for `isset($var) ? $var + 1 : 1`


This is not the case, there is no isset($var) involved - specficially isset($var) would first check if a variable exists and is not null (ISSET_ISEMPTY_CV), it is designed to handle undefined variables, where as most things are not.

Before the increment can happen, the value in the variable must first be read. This leads to the E_WARNING for accessing an undefined variable just like any other, which this RFC seeks to prohibit.

The value is then copied internally, the original value is incrementeted, and the unchanged copy is returned (its value before incrementing).

The only reason this works at all is because an undefined variable read falls back to null, and the increment operator is hardcoded to treat null++ as 1.

That's why if you do this:

$x = $foo++;
var_dump($x);

You get:

Warning: Undefined variable $foo in <path> on line 3
NULL

Mark Randall

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to