NYCPHP Meetup

[nycphp-talk] Is @$arr['key'] ok?

Kenneth Downs ken at secdat.com
Mon Mar 10 09:28:29 EDT 2008


Michael B Allen wrote:
> Is there any preference for:
>
> $val = isset($arr['key']) ? $arr['key'] : null;
>
> vs:
>
> $val = @$arr['key'];
>
> ?
>
>   
Oftentimes I like to check for a key value, and then supply a default if 
it is not there.  It can be useful to make up a routine that does this 
for you all in one step:

$rowsPerPage = arraySafe($options,'rowsperpage',25);
$orderLines = arraySafe($orders,'lines',array());

The function is:

function arraySafe($array,$key,$default='') {
  if(isset($array[$key]))
     return $array[$key];
  else
     return $default;
}


-- 
Kenneth Downs
Secure Data Software, Inc.
www.secdat.com    www.andromeda-project.org
631-689-7200   Fax: 631-689-0527
cell: 631-379-0010




More information about the talk mailing list