Is there a concise way to check if a variable is set, and then echo it without repeating the same variable name?
Instead of this:
<?php
if(!empty($this->variable)) {
echo '<a href="', $this->variable, '">Link</a>';
}
?>
I'm thinking about something in the lines of this C-style pseudocode:
<?php
echo if(!empty($this->variable, '<a href="', %s, '">Link</a>'));
?>
PHP has sprintf, but it doesn't quite do what I was hoping for. If course I could make a method/function out of it, but surely there must be a way to do it "natively"?
Update:
Ternary operations would also repeat the $this->variable
part, if I understood it?
echo (!empty($this->variable) ? '<a href="',$this->variable,'">Link</a> : "nothing");
(bool) ? value : default
) is not an option? – Coop