issetor shorthand function in PHP

This is one of those nifty little functions that you never seem to remember off the top of your head. Basically it is shorthand for when you want to print a variable if it is set or exists. Instead of doing a bunch of is/or checks on your variable, try this instead:

function issetor(&$var, $default = false) {
    return isset($var) ? $var : $default;
}

Usage:

echo issetor($variable);

Or in a HTML-template:

<span class="name"><?=issetor($name)?></span>

Since the function takes two arguments, the variable itself and a default value, you could change the default behaviour if you want.

(Kudos to NikiC at Stackoverflow.)

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>