[nycphp-talk] Accessing non-existent array elements ok?
justin
justin at justinhileman.info
Sun Aug 17 18:29:54 EDT 2008
On Sun, Aug 17, 2008 at 4:57 PM, justin <justin at justinhileman.info> wrote:
>
> if i were you, i'd prob'ly ask if it exists first:
>
> <?php
>
> $result = isset($a['c']) ? $a['c'] : NULL;
>
> /** OR **/
>
> $result = array_key_exists('c', $a) ? $a['c'] : NULL;
>
> ?>
>
Note that the key difference between isset() and array_key_exists() is
the result when $a['c'] *exists* but has a *value* of NULL... Since
we're assigning $result to NULL in either case, we would use isset(),
as it is much faster.
justin
--
http://justinhileman.com
More information about the talk
mailing list