Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

PHP Ternary Operator not working for this code

i would like to use Ternary Operator for the below code..

<?php 
if($users['active'] == 1 ) 
{ 
    echo 'Yes'; 
} 
else 
{ 
    echo 'No'; 
} 
?>

i tried using this code and it does not work..

<?php ($users['active'] == 1) ? echo "Yes" : echo "No";  ?>

what is wrong?

Upvotes: 3

Views: 748

Answers (3)

Felix Kling
Felix Kling

Reputation: 816334

Just for fun a different way:

$msg = array(true => 'Yes', false => 'No');
echo $msg[(users['active'] == 1)];  

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157839

<? if ($users['active']): ?>Yes<? else: ?>No<? endif ?>

Upvotes: 0

Orbling
Orbling

Reputation: 20602

The echo goes outside of it, not in, it (the ternary operator) is returning a value, not a codeblock.

<?php echo ($users['active'] == 1 ? "Yes" : "No"); ?>

Upvotes: 12

Related Questions