On Sat, Feb 26, 2022 at 5:35 PM Mark Randall <marand...@php.net> wrote:

> On 26/02/2022 09:09, Robert Landers wrote:
> > I'd like to propose and implement an RFC that would consider dropping the
> > warning if and only if array access is the only test in ternary and short
> > ternary (?:) operations but first I'd like to start a discussion to see
> if
> > it is worth pursuing. I'd like to target PHP 8.2 or 8.3.
>
> The warning comes from the array key being missing, not from being
> empty. There are many values which are empty which do not emit the warning.
>
> If you want to provide a default should the array key be missing you
> want null coalesce.
>
> $x = $array['foo'] ?? 'somethingelse';
>
>
This is not semantically the same though. A $_POST of a form, for example,
will usually contain an empty value if the form input was empty, but
missing if the form control wasn't in the form (maybe the form control is
injected via Javascript, or conditionally output by the script).

Here is how the code might look currently to handle a default value for an
empty form post that may or may not be sent in the request (ignoring how
insecure this looks):

$name = $_POST['name'] ?: 'Default Name';

Semantically the same to get rid of the warning:

$name = empty($_POST['name']) ? 'Default Name' : $_POST['name'];

I'm not sure your suggestion to set a default applies since we have to do
the empty check anyway, which swallows the warning?

$name = empty($_POST['name'] ?? 'Default Name') ?: 'Default Name';


>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
Robert Landers
Software Engineer
Utrecht NL

Reply via email to