Craig Francis wrote on 02/09/2015 10:06:
On 1 Sep 2015, at 19:17, Rowan Collins <rowan.coll...@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,

Again, you insist on drawing a false equivalence to the different concepts of variable and array key. This makes the following sentence really easy to dismiss:


  I would have simply used isset() without thinking (this being a problem if 
the variable was set to NULL).

No, it wouldn't be a problem if a VARIABLE was set to null, because there is no practical difference between an unset variable and one set to null, so you never need to ask which you have.

It *would* be a problem if you were testing for the existence of an array key. There should probably be a prominent link to array_key_exists() and property_exists() on the isset() manual page


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).

This is a more reasonable point, and I was pondering whether it would be possible to allow a more natural syntax.

It would probably require a lot of "magic" in the engine, though, because it would have to parse exists($foo['bar']['baz']) in some special way to know that it needed to first find the value of $foo['bar'], and then do an existence check for key 'baz', rather than just treating the whole thing as an expression to resolve before exists() was called. Evaluating things like exists(foo()[ bar() ]) correctly might get messy, as would giving a meaningful result or error if you wrote exists($foo['bar'] + 1).


And the second one does not really check for the existence of $_GET, which in 
this case is probably ok.

I'm not really sure what you mean by this.


In summary, I don't think any language change is needed here, but documentation needs to be improved. I will have a go at drafting a new intro for the isset() page when/if I get time.

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to