php - is there an even shorter way to print whichever of two values exists? -
i using (essentially) method print whichever of 2 values non-null, 1 having priority on other:
<?php $a = "high priority text may empty or null"; $b = "low priority fallback may empty or null"; ?> value want echo is: <?=($a)?$a:$b?>.
this can long, in code, when looks more like:
the value want echo is: <?=($_get['the_insanely_long_name'])?$_get['the_insanely_long_name']:$old_val['the_insanely_long_name']?>
in languages, following type of statement works, not in php:
the value want echo is: <?=$_get['the_insanely_long_name']||$old_val['the_insanely_long_name']?>
is there similar trick in php further shorten or simplify code?
in php 5.3+ can use ?:
operator (manual):
since php 5.3, possible leave out middle part of ternary operator. expression expr1 ?: expr3 returns expr1 if expr1 evaluates true, , expr3 otherwise.
<?= ($a) ?: $b; ?>
Comments
Post a Comment