I want to get a value from the session, but use a default if it is not defined. And ofcourse I want to circumvent the PHP notice.
You can write a function that does this
JavaScript
x
function get(&$var, $default){
if(isset($var)) return $var;
return $default;
}
echo get($foo, "barn");
$foobar = "foobar";
echo get($foobar, "ERROR");
Is there a way to do this without defining this function in every file?
Advertisement
Answer
From PHP 7, you can now use the null coalescing operator :
JavaScript
$var ?? $default
that does exactly like your function