简体   繁体   中英

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?

echo超出它,而不是,它(三元运算符)返回一个值,而不是一个代码块。

<?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 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM