There is one other difference that has not been pointed out yet. Consider the following:
$array = null; array_key_exists( 0, $array['key'] ); isset( $array['key'][0] );
In this case, array_key_exists will cause an error, because $array['key'] doesn't exist, but isset() won't because it doesn't matter what part of the variable isn't set, so long as it's not set it will return false.
That was the main reason I recommended this for E:OpenID. If you look at the code in the patch (https://gerrit.wikimedia.org/r/71258), this was the original.
array_key_exists( 'http://axschema.org/namePerson/first', $ax )
Meaning, it's possible that $ax[...] isn't set, meaning trying to see if the index 0 is in $ax[...] will cause an error.
*-- * *Tyler Romeo* Stevens Institute of Technology, Class of 2016 Major in Computer Science www.whizkidztech.com | tylerromeo@gmail.com
On Tue, Jul 2, 2013 at 2:24 PM, Thomas Gries mail@tgries.de wrote:
Am 02.07.2013 18:47, schrieb Brion Vibber:
On Tue, Jul 2, 2013 at 9:40 AM, Thomas Gries mail@tgries.de wrote:
PHP codestyle question re. isset( $array['key'][0] ) versus array_key_exists( 0, $array['key'] )
On a recent patch [1], we had a discussion what is - or may be - better
- isset( $array['key'][0] )
- array_key_exists( 0, $array['key'] )
In my opinion, isset() is usually clearer and "more natural", in part because you don't have to guess/memorize the order of parameters (which
is
haystack again, and which is needle?).
There is however a functional difference: if your array can contain null
as
a valid value, then isset() can give you a false negative. So beware...
-- brion
In the present case of E:OpenID, I need to check the data of an array which has been read in from the Provider. In my view, it appears to be more secure to use array_key_exists( ).
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l