简体   繁体   English

我如何使for语句具有其他

[英]How can I make a for statement have an else

I have a databse that has food reviews on it, and I want to show if a food is vegetarian or not. 我有一个上面有食物评论的数据库,我想表明食物是否是素食主义者。 I want to add a echo "No"; 我想添加回声“否”; but I don't know how. 但我不知道如何

I've tried using else, but it doesn't show. 我尝试使用其他,但未显示。 I've also tried <?php for($x=1; 我也试过<?php for($x=1;

Here is my code: 这是我的代码:

<p>Vegetarian: <span class="sub_heading"><?php for($x=0; $x < $find_rs['Vegetarian']; $x++)
                    {
                        echo "Yes";
                    }
                        ; ?></span></p>

I want it to show no if it is 0 and yes if it is 1 我希望它在显示为0时显示否,在显示1时显示是

<p>
    Vegetarian: 
    <span class="sub_heading"><?= $find_rs['Vegetarian'] ? "Yes" : "No"?></span>
</p>

Simply add an if conditional inside of your loop that checks against the value of $x : 只需在循环中添加if条件语句,以检查$x的值:

for ($x=0; $x < $find_rs['Vegetarian']; $x++) {
    if ($x == 1) {
        echo "Yes";
    }
    else if ($x == 0) {
        echo "No";
    }
    else {
        echo "x wasn't either 0 or 1 -- x was $x";
    }
}

Note that the final else condition will output the value of $x due to the use of double-quotes. 注意,由于使用了双引号,最终的else条件将输出$x的值。

if the condition only 2, just using if else statement, 如果条件只有2,只要使用if else语句,

<?php
for($x=0; $x < $find_rs['Vegetarian']; $x++){
     if($x == 1){echo 'Yes';}else{echo 'No';}
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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