[nycphp-talk] Is @$arr['key'] ok?
Michael B Allen
ioplex at gmail.com
Sat Mar 8 21:14:13 EST 2008
On 3/8/08, Hans Zaunere <lists at zaunere.com> wrote:
> > Is there any preference for:
> >
> > $val = isset($arr['key']) ? $arr['key'] : null;
>
>
> This is preferred - it's actually doing something correct.
>
> > vs:
> >
> > $val = @$arr['key'];
>
> This is a hack, which causes an error, which you're then suppressing
>
> Marcus and I discussed a shortcut for the first example at the last Montreal
> PHP Conference, but I don't think it's been implemented yet.
Actually I've been using the following function a lot too:
function _array_val($a, $k, $d = null) {
if (array_key_exists($k, $a))
return $a[$k];
if (func_num_args() == 3)
return $d;
throw new Exception("No such array key: $k");
}
--8<-- example --8<--
$val = _array_val($arr, 'key', false);
Of course it's a little slower but it makes for clean code.
Mike
--
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/
More information about the talk
mailing list