NYCPHP Meetup

NYPHP.org

[nycphp-talk] Handling empty values in hashes

paul at devonianfarm.com paul at devonianfarm.com
Thu Jul 17 14:58:00 EDT 2008


> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20080717/f2b0e2c9/attachment.html>


More information about the talk mailing list