DrCustUmz
DrCustUmz

Reputation: 79

Ternary Operator in a php print?

I am trying to use a ternary operator in a print statement like so:

print'<span class="mailbox-attachment-icon"><i class="fa fa-'. $extension == 'zip' ? 'yes' : 'no'  .'"></i></span>';

It is breaking my page every time. I have attempted moving around ''s with no luck. Yes $extension is defined.

How can I get this to work?

Upvotes: 0

Views: 199

Answers (1)

Liftoff
Liftoff

Reputation: 25392

Your code is being evaluated left to right. You have to enclose that ternary clause in parentheses to ensure it is evaluated before being appended to the string.

print'<span class="mailbox-attachment-icon"><i class="fa fa-'. ($extension == 'zip' ? 'yes' : 'no')  .'"></i></span>';

Upvotes: 2

Related Questions