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'] )
This is still unclear after consultation of these pages
* http://php.net/isset http://php.net/isset, * http://php.net/array_key_exists and * https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Pitfalls .
... I would prefer/array_key_exists/ .
Perhaps someone can explain why which one of the methods should be preferred ? The https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Pitfalls section should be improved with the explanation.
[1] https://gerrit.wikimedia.org/r/#/c/71258/ see comments for patchset 8
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
On Wed, Jul 3, 2013 at 12:47 AM, Brion Vibber bvibber@wikimedia.org wrote:
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...
btw it'll silently return false without any error and make it sometimes difficult to debug when you mistyped isset( $array['key'][0] ) as isset( $arrray['key'][0] ) accidentially...
-Liangent
-- brion _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
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( ).
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
Le 02/07/13 18:40, Thomas Gries a écrit :
- isset( $array['key'][0] )
- array_key_exists( 0, $array['key'] )
isset() acts on the value while the second look for a key. So they are slightly difference.
I use array_key_exists() which is explicit and well known. I tend to forget isset() is really isset_and_not_null(), part of the reasons I dislike PHP :-]
wikitech-l@lists.wikimedia.org