Is there any way to shorten this code ?
<?= isset($email)?$email:''; ?>
I feel it's kind of stupid to repeat $email. I've tried
<?= isset($email)?:''; ?>
But it echoes the boolean coming from isset instead.
<?= isset($email)?$email:''; ?> // is the shortest way to do it.
You could write a custom function:
function safeEcho(&$var) {
if (isset($var))
return $var;
return null;
}
and call this function:
<?= safeEcho($var) ?>
safeEcho. 2) This isn't as "safe" as you think - it will issue E_NOTICE if a $email isn't setThe only way of "shorting" that, is a custom function.
function get(&$email) { // <- Note, there must be a reference!!!
return isset($email) ? $email : '';
}
<?= get($email); ?>
If you pass $email without a reference, then isset() will issue a E_NOTICE if a variable isn't set. This is because you pass to isset() a copy of undefined variable, not a variable itself.
Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.
@$emailto say "this might not be set but I don't care."