Is array_key_exists superfluous?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryos
Forum Newbie
Posts: 16
Joined: Tue Feb 14, 2006 4:55 pm

Is array_key_exists superfluous?

Post by ryos »

This post is pure curiosity. Is there any reason to use:

Code: Select all

array_key_exists ($key, $array);
...instead of:

Code: Select all

isset ($array[$key]);
...beyond, of course, stylistic preference?

TIA for your opinions.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I believe array_key_exists() will search an entire multi-dimensional array for a key.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

arborint wrote:I believe array_key_exists() will search an entire multi-dimensional array for a key.
I don't see that in the manual and I was not under that impression myself :)

The last comment in the manual reveals that this function is slower than using isset(). I seem to interchange between the two quite a lot which is bad I guess but array_key_exists() could be used in a more contextual sense when you want to make it clear you're looking for an ite in a container, not just the presence of a variable.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

yeah, array_key_exists() may spit 'undefined vars' error.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isset() fails when a the value of the element is null. array_key_exists() does not look at the value.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g.

Code: Select all

$arr = array(1=>null);
echo 'isset: ', isset($arr[1]) ? 'yes':'no', "\n";
echo 'exists: ', array_key_exists(1, $arr) ? 'yes':'no', "\n";
isset: no
exists: yes
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

off topic, but how come you send multiple string parameters to echo instead of just concatenating? Is there any benefit or is it just preference?

Code: Select all

echo 'this ', 'is ', 'a ', 'string';
instead of:

Code: Select all

echo 'this ' . 'is ' . 'a ' . 'string';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

less processing required.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

feyd wrote:less processing required.
8O

You learn something new every day!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Kieran Huggins wrote:
feyd wrote:less processing required.
8O

You learn something new every day!
Logical when you think about it. Do the concatenation way and PHP needs to write new values to memory before adding it to the buffer and flushing. Use commas and PHP just uses the values already in memory.
Post Reply