Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

toneee's avatar

Blade if else on single line

Hi,

I am trying to get an if else statement on a single line in a blade. is this possible? So to compress the below on to a single line.

@if ($var === "hello")
    Hi
@else
    goodbye
@endif
0 likes
10 replies
El_Matella's avatar
Level 5

I think that you could try:

{{ $var === "hello" ? "Hi" : "Goodbye" }}
27 likes
bashy's avatar

Yeah use the above. I normally put brackets around it just so I can tell it's that (if longer).

You can also do this if you just wanted to check if it's set.

{{ $name or 'Default' }}
8 likes
toneee's avatar

Thanks, works perfectly, I think staring too long at the ternary operators gave me a blank :)

GuntarV's avatar

@BASHY - Great, thanks. Very short and sweet! I will be using this syntax a lot. Performance wise, is this one better or about the same as the other syntax's, or does it really matter?

bashy's avatar

@guntarsv I wouldn't worry about the performance of that vs shorthand if else. But you should use ?? as Cronix pointed out. It's been changed from Blade to native PHP.

Cronix's avatar
@if(someCondition) output this text @endif
1 like
minaFaragAmin's avatar

didn't work when using array search as first part of the equation

Please or to participate in this conversation.