dsfds2343 2017-02-02 08:53
浏览 26
已采纳

在php中使用逆条件顺序在if else条件下使用OR

I am trying to get "OK" if my condition False, i want to keep same if else order as given example, don't advice to move "else" content in "if" and "if" content in "else",

 <?php

    //Case 1 Both False
    //$point = 1001; //more than 100
    //$flag  = 1;   //flag is = 0 

    //Case 2 First False Second True
    //$point = 1000; //less than 1000
    //$flag  = 0;   //flag is = 0 

    //Case 3 First TRUE Second FALSE
    //$point = 100; //less than 100
    //$flag  = 1;   //flag is = 1 

    $point = 1001;
    $flag  = 1;   
    ?>

    <div class="box border">
    <div class="box-title">
    <?php if($flag == 0 || $point < 1000) { ?>
    <h4>Not OK</h4>
    <?php } else { ?>
    <h4>OK</h4>
    <?php }?>
    </div>

in above script //Case 1 is working but Case 2 and Case 3 both not working with OR operator

I want all 3 cases work in same pattern

  • 写回答

2条回答 默认 最新

  • douqiao1413 2017-02-02 10:33
    关注

    After reading your comments:

    It seems you want to show some score (in the else-branch of an if-then-else) if the user has equal/more than 1000 points or you toggle a flag (which basically means "always show points").

    Normally you'd be doing that in the if-branch:

    if ($flag || $point >= 1000)
    {
        echo "OK";
    } else {
        echo "Not OK";
    }
    

    Since you want the Not OK to be in the if-Branch you need to inverse the condition, doing so means inverting every part of the condition and the operators aswell:

    • $flag becomes !$flag
    • $point >= 1000 becomes $point < 1000
    • || becomes &&

    Result:

    if (!$flag && $point < 1000)
    {
        echo "Not OK";
    } else {
        echo "OK";
    }
    

    Writing this into a truth-table:

    flag        point       result
    0           < 1000      Not OK
    1           < 1000      OK
    0           >=1000      OK
    1           >=1000      OK
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮