Take this:
$data = array('one'=>'1','three'=>'3');
Now which is better?
This:
echo @$data['two'];
or this:
function val($data,$key,$default){
if(isset($data[$key])){
return $data[$key];
}
return $default;
}
echo val($data,'two','');
or this:
echo isset($data['two'])?$data['two']:'';
or something else?
avoiding the notice: Notice: Undefined index: two in document on line #num
which one is the most efficient, and which one should I use?
I am wondering that maybe the super-slow error suppressing might be faster than having a dedicated function?
p.s. Lots of answers seem to assume that I am doing this as a form of optimization, this is not true, I am asking the “efficiency” part out of curiosity and the “which should I use” part because I need to use something and I want to know what I should default to.
p.p.s. most efficient and which used will most likely be different
This is common sense answer
“@” symbol will suppress PHP-generated error messages. suppress, notice will occure and error handling function will be called.
issetis part of the language construct, therefore it is much faster.Use Ternary Operators
isset($dat['index']) ? $data['index'] : null, because it looks clean and does not trigger error handling