Re: [Early Feedback] Pattern matching

From: Date: Tue, 25 Jun 2024 21:10:22 +0000
Subject: Re: [Early Feedback] Pattern matching
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


On 25 June 2024 21:24:32 BST, Rob Landers <[email protected]> wrote:
>The only way you’d observe this (that I can think of) is by performing a for-each loop over
>the array. 

There are many ways you can observe the difference between an absent key and a null value; here are
just a handful off the top of my head: 

- array_key_exists() (that's literally its purpose!)
- array_keys()
- count() (if the array held "an infinite number of nulls", we should return infinity for
every array!)
- json_encode()
- print_r(), var_dump(), var_export()
- extract()

It may be questionable to give meaning to the difference in some of these cases, but different it
definitely is. 


> If we don't like it, we can always create an RFC to treat non-existent keys as an error
> instead of a warning.

I believe that is the explicit intention or desire of those who raised it from Notice to Warning. It
would certainly prevent some bugs where a typo leads to the wrong key being accessed.

Personally, I'd like to see a few use cases catered for first, like $counters[$key]++ and
$groups[$key][] = $value; Perhaps by introducing some equivalent of Python's
"defaultdict". Because I do agree that the current behaviour is useful sometimes (even if
I disagree in how to describe it).


Regards,
Rowan Tommins
[IMSoP]


Thread (79 messages)

« previous php.internals (#123845) next »