簡體   English   中英

更好的方法來做這個簡單的IF ELSE?

[英]Better way to do this simple IF ELSE?

有沒有更好/更簡單/更合適的方式來實現這個簡單的PHP? 我想回聲某些東西,除非變量等於123。

   <?php 
        if ($abc=="123") echo ""; // do nothing
        else echo "something"; 
    ?>
echo ($abc=="123") ? "" : "something";

三元運算符

因為I want to echo something unless the variable is equal to 123.使用!=邏輯運算符。

if ($abc != "123") echo "something"; 

評論回應

您正在檢查$ abc值是123還是456

現在,如果$abc值為456條件, if工作原理如下。

if($abc != '123' || $abc != '456') // if will be  (true || false) which is false

您可以簡單地執行以下操作:

if ($abc != "123") echo "something";

最簡單的方法:

'123' == $abc or print 'something';

請注意,在這種方法中,您只能使用print 原因echo '...' 不能計算為bool ,與print不同。

更新資料

多變量排除示例:

if ('123' != $abc and '456' != $abc) print 'something';

// this code will do the same stuff
'123' == $abc or '465' == $abc or print 'something';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM