簡體   English   中英

我如何使for語句具有其他

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

我有一個上面有食物評論的數據庫,我想表明食物是否是素食主義者。 我想添加回聲“否”; 但我不知道如何

我嘗試使用其他,但未顯示。 我也試過<?php for($x=1;

這是我的代碼:

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

我希望它在顯示為0時顯示否,在顯示1時顯示是

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

只需在循環中添加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";
    }
}

注意,由於使用了雙引號,最終的else條件將輸出$x的值。

如果條件只有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