php - Are there any way to check exitency of an array key inside the function -


i have project need echo $variable['key'];

some times $variable['key'] not exist. , variables creates errors when echo $variable['key']; directly.

my current method

echo (isset($variable['key'])) ? $variable['key'] : ''; 

and not efficient way of doing that. not want write keys 2 times.

i need function. checks $varible['key']; inside function , returns value of it.

function get_ifisset(&$var = null){ if(isset($var)){ return $var; } }  echo get_ifisset($vars['key']); 

but because of $variable['key'] not exist can not send them inside function

this kind of usage throws error "undefined key".

also following function approach dont like.

function get_ifisset($var, $key){ if(array_key_exists($key, $globals[$var])){ return $globals[$var][$key]; } } 

i learn there way check exitency of array key inside function.

property_exists(); array_key_exists(); isset();

you can pass array, key , default value function:

public function getvalue($array, $key, $default = '') {     return isset($array[$key]) ? $array[$key] : $default; }  $this->getvalue($variable, 'key'); 

also, experience function get_ifisset(&$var) not throw error (i have not set default null).


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -