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
function get(&$var, $default){
if(isset($var)) return $var;
return $default;
}
echo get($foo, "bar\n");
$foobar = "foobar";
echo get($foobar, "ERROR");
Example in action
Is there a way to do this without defining this function in every file?
You can define it in one script and then require_once
that script in your other scripts
You could also just use the ternary operator:
$myVar = isset($var)?$var:$default;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With