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

nafeeur10's avatar

Ternary Operator in PHP

Generally we can use ternary operator like this:

(Condition) ? (Statement1) : (Statement2);

But I want just one. For example:

if($variable)
{
    echo "YES";
}

I want just the if condition in ternary.

Like this: $variable ? echo "YES";

But it's giving me error:

expected colon
0 likes
4 replies
tykus's avatar
echo $variable ? 'YES' : '';
1 like
nafeeur10's avatar

@tykus,

If this value has then I just want to print yes. I don't want to go else condition. Is it possible?

tykus's avatar

The ternary operator expects three operands.

1 like

Please or to participate in this conversation.