0

Is there any way to write this

echo $foo ? 'bar' : '';

but without the false part? Just like this:

echo $foo ? 'bar';

and nothing would be echoed if $foo is false automatically?

I often find that I just want to echo something if it's true but don't care about an else statement :)

SeaBass
  • 1,584
  • 4
  • 20
  • 46
  • [elvis operator](https://stackoverflow.com/questions/1993409/operator-the-elvis-operator-in-php) perhaps... `?:` – Professor Abronsius Nov 04 '21 at 22:36
  • 2
    I would think a simple and boring `if ($foo) echo 'bar';` would do? – KIKO Software Nov 04 '21 at 22:43
  • Yeah but sometimes I want to do it in an echo and not a separate if statement. ?: only returned true for me not the variable. – SeaBass Nov 04 '21 at 22:45
  • @ProfessorAbronsius ah, that one just returns the left side if it’s true, otherwise the right, which is useful at times too. I want to return the right side only if left is true. – SeaBass Nov 04 '21 at 22:52

2 Answers2

2

This won't work with echo, but using print() your can do:

$foo && print('bar');

I just discovered that parentheses aren't even needed for print, so a bit shorter would be:

$foo && print 'bar';

Please also note @IMSoP's striking comment below, about parentheses for echo and print:

Note also that neither echo nor print is a function, and using parentheses like you have here is a habit to avoid, as it can sometimes be misleading - e.g. print('hello') && true; will output "1", not "hello" - the parentheses mean nothing, and the value passed to print is the result of 'hello' && true

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • Thanks! I need it to work in echo. Or laravel/blade style class=”{{ $foo ? ’bg-gray’ : ’’ }} without the last part :) I guess I’d use the same with js/alpine. – SeaBass Nov 04 '21 at 22:50
  • This is one of the rare cases when [the alternative "and" operator](https://stackoverflow.com/questions/2803321/and-vs-as-operator) is useful: because it has low precedence, you can write things like `$foo = someFunc() and print 'bar';` rather than needing parentheses for `($foo = someFunc()) && print 'bar';` – IMSoP Nov 04 '21 at 22:50
  • @SeaBass You mix template engine syntax with PHP syntax. So you asked the wrong question. – cottton Nov 04 '21 at 22:53
  • 1
    Note also that neither `echo` nor `print` is a function, and using parentheses like you have here is a habit to avoid, as it can sometimes be misleading - e.g. `print('hello') && true;` will output "1", not "hello" - the parentheses mean nothing, and the value passed to `print` is the result of `'hello' && true` – IMSoP Nov 04 '21 at 22:54
  • @cottton no I don’t mix it. I’d use it in plain php too. – SeaBass Nov 04 '21 at 22:56
  • @IMSoP I've incorporated your remark about parentheses into my answer. – Decent Dabbler Nov 04 '21 at 23:06
2

You could write your own function to do this:

function when($returnContent,$content)
{
  return $returnContent ? $content : '';
}

That is simple enough, you can use it like this:

echo 'text before ' . when($foo, 'middle text ') . ' text at end.';

Perhaps not what you had in mind, but it does the job.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33