I use PHP as a template language in my view layer, Is it possible to make the following a little cleaner and more concise?
JavaScript
x
//in temp.phtlm
<?= (isset($user['name'])) ? $user['name'] : null; ?>
Unfortunately, we can not even define a function for that:
JavaScript
// as you know yet a notice is generated when calling a function with an undefined parameter
function echo($var)
{
return (isset($var)) ? $var : null;
}
Advertisement
Answer
Using @
operator is the shortest alternative.
JavaScript
<?= @$user['name']; ?>
Since PHP 7 you can also use the ??
operator.
JavaScript
<?= $user['name'] ?? null; ?>
Which does exactly what the @
operator does, though a bit longer.