Re: PHP 7.1 - Address PHPSadness #28?
| From: | Craig Francis | Date: | Wed, 02 Sep 2015 09:06:05 +0000 |
| Subject: | Re: PHP 7.1 - Address PHPSadness #28? | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88006@lists.php.net to get a copy of this message | ||
On 1 Sep 2015, at 19:17, Rowan Collins <rowan.collins@gmail.com> wrote:
> I'm still not sure how exists() would be anything other than an alias for
> array_key_exists().
I think that is the case as well (ish).
But considering that isset() seems to be the function that is used most of the time, I want to
consider why that is the case.
Before last week, if someone asked me to write some code to see if a variable (or key) exists, I
would have simply used isset() without thinking (this being a problem if the variable was set to
NULL).
Whereas, if I was told to check if the variable was NULL, I would do an "$a === NULL" or
is_null().
It might just be due to the functions name length or usage, while wanting to avoid undefined
variable notices:
<?php
$search = (isset($_GET['q']) ? $_GET['q'] : '');
$search = (array_key_exists('q', $_GET) ? $_GET['q'] : '');
?>
The second one does also represent the variable in a different way (grep for
"$_GET['q']", where it only appears 3 times in that code).
And the second one does not really check for the existence of $_GET, which in this case is probably
ok.
Craig
On 1 Sep 2015, at 19:17, Rowan Collins <rowan.collins@gmail.com> wrote:
> On 01/09/2015 10:29, Craig Francis wrote:
>> Personally I still like the idea of an exists(), because I feel that is how many
>> programmers treat and use the isset() function - simply because they do use NULL as a valid value,
>> and either haven't read the manual, or forget the exception that is mentioned on line 1
>> (something I've done a couple of times).
>
> I'm still not sure how exists() would be anything other than an alias for
> array_key_exists().
>
> Once again, this is NOT about "using NULL as a valid value"; it's about
> "using a variable's NON-EXISTENCE as a valid state". I think that is the fundamental
> mistake people make - they think the "quirk" is that isset() returns false for a null
> value, when that is the most rational part of it. The "quirk" is that you don't get a
> warning when passing a completely undefined variable to isset().
>
> Obviously, that's not something that's likely to change, but as you say, what we are
> really testing is "the value pointed at by some identifier". In an array, that identifier
> can be dynamic, but in plain variable scope, that identifier is something you the programmer have
> defined long before the program executed, so its existence is never in question.
>
> Looked at that way, the handling of unitialised variables is just a side-effect of the handling
> of NULLs, since all unitialised variables have a default/implicit value of NULL.
>
> Regards,
>
> --
> Rowan Collins
> [IMSoP]
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>