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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -