开发者

PHP Ternary Operator not working for this code

开发者 https://www.devze.com 2023-02-24 08:21 出处:网络
i would like to use Ternary Operator for the below code.. &l开发者_运维问答t;?php if($users[\'active\'] == 1 )

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

&l开发者_运维问答t;?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?


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"); ?>


Just for fun a different way:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消