The shortest way to echo out stuff in views in PHP - when not using template engines - is, afaik, this one:
<?php if (!empty($x)) echo $x; ?>
For a deeper explanaition why using !empty
is a good choice please look here.
Is it possible to write this without writing the variable name twice (like in other languages), something like
!echo $x;
or
echo? $x;
echo @$x;
It's not exactly the right way to do it, but it is shorter. it reduces the need to check if $x exists since @ silences the error thrown when $x == null;
edit
echo empty($x) ? "" : $x;
is a shorter way, which is not really that much shorter nor does it solve your problem.
guess the other answers offer a better solution by addressing to make a short function for it.
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