> One right way to do this is:
> 
> if (array_key_exists($key, $array)) {
   > // Life is good.
   > $value = $array[$key];
> } else {
   > // Erm, it doesn't exist.  Now what?
   > // Typically either set to NULL or throw error.
> }

    For a long time I've used

isset($array[$key])

how's that different from array_key_exists?  The article at

http://gen5.info/q/2008/07/17/the-semantics-of-dictionaries-maps-and-hashtables/

tries to capture a similar logic into a single function,  array_get()

Overall I think logic like the code sample you give should be condensed into a 
function for several reasons:  the most serious is that duplicated logic can 
hide errors.
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php

Reply via email to