I have a project that I need to echo $variable['key'];
Some times $variable['key'] is not exist. And Those variables creates errors when I echo $variable['key']; directly.
My current method is
echo (isset($variable['key'])) ? $variable['key'] : '';
And this is not very efficient way of doing that. I do not want to write all keys two times.
I need a function. Basically That checks the $varible['key']; inside the function and returns the value of it.
function get_ifisset(&$var = null){
if(isset($var)){ return $var; }
}
echo get_ifisset($vars['key']);
but because of $variable['key'] is not exist I can not send them inside to the function
This kind of usage throws error which is "undefined key".
Also following function is an another approach that I dont like.
function get_ifisset($var, $key){
if(array_key_exists($key, $GLOBALS[$var])){ return $GLOBALS[$var][$key]; }
}
I would like to learn Are there any way to check exitency of an array key inside the function.
property_exists(); array_key_exists(); isset();
modified @Justinas function:
so you can use it on multidimensional arrays