Re: [RFC] Undefined Variable Error Promotion

From: Date: Fri, 18 Feb 2022 08:51:01 +0000
Subject: Re: [RFC] Undefined Variable Error Promotion
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-117059@lists.php.net to get a copy of this message
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

« previous php.internals (#117059) next »