Don P
Don P

Reputation: 63728

PHP ternary operator giving an unexpected T_echo

My ternary is returning an error. Am I forgetting some basic rule or quirk about using PHP ternaries with echos?

isset($tag) ? 
    echo '<a href="#">' . $tag['firstname'] . '</a>' : null;

The above ternary returns the following error:

Parse error: syntax error, unexpected T_ECHO in /classes/Photo.php on line 216

Upvotes: 1

Views: 1686

Answers (2)

Magicianeer
Magicianeer

Reputation: 2180

Use print() instead of echo when you need to display text within an expression.

Upvotes: 2

Rukmi Patel
Rukmi Patel

Reputation: 2561

you should write like this:

echo (isset($tag) ? '<a href="#">' . $tag['firstname'] . '</a>' : '');

Upvotes: 17

Related Questions