26

What's the best, preferred way of writing if shorthand one-liner such as:

expression ? $foo : $bar

Plot twist: I need to echo $foo or echo $bar. Any crazy tricks? :)

Wordpressor
  • 7,173
  • 23
  • 69
  • 108
  • 1
    *Ternary statement*, not *shorthand if*. What's the actual question here? – Phil Nov 27 '13 at 03:16
  • 1
    echo expression ? $foo : $bar; – Felix Kling Nov 27 '13 at 03:17
  • 1
    @Phil: *Conditional expression*, not *ternary statement*. :) Were it a statement, `echo` would work in the branches. And the only reason you can get away with calling it the ternary anything is that there's currently only one ternary operator. "Ternary" meaning having to do with three, though...it's not a very helpful name. :) – cHao Nov 27 '13 at 03:22
  • @cHao Ah yes, `s/statement/operator`. It just bothers me when I hear *shorthand if* – Phil Nov 27 '13 at 03:26
  • Nowadays we have the elvis '??' operator https://stackoverflow.com/a/1993455/560287 – John Magnolia Apr 16 '21 at 12:37

4 Answers4

64
<?=(expression) ? $foo : $bar?>

edit: here's a good read for you on the topic

edit: more to read

Community
  • 1
  • 1
Dave
  • 2,774
  • 4
  • 36
  • 52
  • 4
    The parentheses are redundant – Phil Nov 27 '13 at 03:22
  • I've never seen PHP fail to evaluate the conditional expression, no matter how complex, sans a pair of wrapping parentheses – Phil Nov 27 '13 at 03:27
  • Don't mind me, I'm just a stickler for unnecessary syntax (like using parentheses in ternaries and `include` / `require` statements) – Phil Nov 27 '13 at 03:32
  • 1
    If you're in the habit of using `and` and `or` in your conditions rather than `&&` and `||`, it can trip you up. But only because the wordy operators have different precedence. – cHao Nov 27 '13 at 03:33
  • what can i do if I want to echo only when true and do nothing on false? – pavitran Feb 16 '17 at 17:57
  • $bar = '' @pavitran – Lightningsoul Apr 03 '18 at 12:07
  • Short-tags are disabled by default and are depreciated and it's advice to NOT use this any longer. http://php.net/manual/en/language.basic-syntax.phptags.php – Melroy van den Berg Jan 06 '19 at 01:24
  • @danger89 = isn't deprecated... in 5.4 on that very page you linked 'The tag = is always available regardless of the short_open_tag ini setting.' – Adam Mar 05 '19 at 13:26
  • @Adam Indeed, but that is only for version 5.4. Otherwise there is stated: ` (which is discouraged since it is only available if enabled using the short_open_tag php.ini ` – Melroy van den Berg Mar 11 '19 at 15:59
  • @danger89 it is from 5.4 onwards. is discouraged since it relies on short_open_tag being enabled as you said. = even in 7.X doesn't rely on this option being enabled therefore there is no reason for it to be discouraged. – Adam Mar 11 '19 at 19:55
12
echo (expression) ? $foo : $bar;
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
A.M.N.Bandara
  • 1,490
  • 15
  • 32
7

The ternary operator evaluates to the value of the second expression if the first one evaluates to TRUE, and evaluates to the third expression if the first evaluates to FALSE. To echo one value or the other, just pass the ternary expression to the echo statement.

echo expression ? $foo : $bar;

Read more about the ternary operator in the PHP manual for more details: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

0

Great answers above, and I love when programmers ask questions like this to create clear, concise and clinical coding practices. For anyone that might find this useful:

<?php

// grabbing the value from a function, this is just an example
$value = function_to_return_value(); // returns value || FALSE

// the following structures an output if $value is not FALSE
echo ( !$value ? '' : '<div>'. $value .'</div>' ); 

// the following will echo $value if exists, and nothing if not
echo $value ?: '';
// OR (same thing as)
echo ( $value ?: '' ); 

// or null coalesce operator
echo $value ?? '';
// OR (same thing as)
echo ( $value ?? '' );

?>

References: