Proposal: Reconsideration of PHP's Undefined Array Key Behavior (Pre-PHP 8 Syntax)
Salvatore Forino <[email protected]> Wed, 9 Jul 2025 14:39:09 +0200
| Newsgroups | gmane.comp.php.general |
|---|---|
| Message-ID | <CAOA8gEy=HdUggdd7e32M3bXzxFzLGirvTfaFxaPw+GHT9PKCLQ@mail.gmail.com> |
--000000000000abf1b906397e5d9e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello everyone, I'm opening this discussion to gather opinions and support regarding a change introduced in PHP 8 and later versions: the stricter handling of undefined array keys. For years, accessing an undefined array key would result in a Notice. While technically not an ideal practice, it was a very common pattern used by countless developers, often for checking the existence of a key and simultaneously using its value if present, or assigning a default if not. Since PHP 8, accessing an undefined array key now throws a Warning (or even an Error in some contexts, depending on the setup and how the code interacts with error handling). While I understand the rationale behind this change=E2=80=94promoting stricter code and preventing potential bugs= =E2=80=94my experience, and that of many developers I've spoken with, suggests that the old behavior, despite its "imperfection," was often more practical, concise, and elegant for everyday coding. I've had numerous conversations with developers across various projects and skill levels, and a recurring theme is that many find the forced isset() or null coalescing operator (??) in every single instance to be verbose and, frankly, less readable in scenarios where the old Notice was perfectly acceptable and manageable. This often leads to code that is more cluttered without necessarily adding significant value in terms of bug prevention for well-understood use cases. It feels like a change pushed by a few for the many, without a broader consensus from the PHP development community. Given that PHP is a language used by millions globally, I believe such a fundamental shift in how we interact with arrays should ideally reflect the preferences of a wider majority. Therefore, I propose we open a democratic discussion and potentially a vote on this matter. --000000000000abf1b906397e5d9e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div> <div> <p>Hello everyone,</p> <p>I'm opening this discussion to gather opinions and support regarding= a change introduced in PHP 8 and later versions: the stricter handling of = undefined array keys.</p> <p>For years, accessing an undefined array key would result in a Notice. Wh= ile technically not an ideal practice, it was a very common pattern used by= countless developers, often for checking the existence of a key and simult= aneously using its value if present, or assigning a default if not.</p> <p>Since PHP 8, accessing an undefined array key now throws a Warning (or e= ven an Error in some contexts, depending on the setup and how the code inte= racts with error handling). While I understand the rationale behind this ch= ange=E2=80=94promoting stricter code and preventing potential bugs=E2=80=94= my experience, and that of many developers I've spoken with, suggests t= hat the old behavior, despite its "imperfection," was often more = practical, concise, and elegant for everyday coding.</p> <p>I've had numerous conversations with developers across various proje= cts and skill levels, and a recurring theme is that many find the forced is= set() or null coalescing operator (??) in every single instance to be verbo= se and, frankly, less readable in scenarios where the old Notice was perfec= tly acceptable and manageable. This often leads to code that is more clutte= red without necessarily adding significant value in terms of bug prevention= for well-understood use cases.</p> <p>It feels like a change pushed by a few for the many, without a broader c= onsensus from the PHP development community. Given that PHP is a language u= sed by millions globally, I believe such a fundamental shift in how we inte= ract with arrays should ideally reflect the preferences of a wider majority= .<br> Therefore, I propose we open a democratic discussion and potentially a vote= on this matter.</p> </div> </div> --000000000000abf1b906397e5d9e--