Use OR within if else condition by inverse condition order in php

前端 未结 2 1438
情歌与酒
情歌与酒 2021-01-29 15:55

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 \"els

相关标签:
2条回答
  • 2021-01-29 16:27

    I tested this code and it works.

    if(!($flag != 0 || $point >= 1000)){
        echo "Not OK";
    }else{
        echo "OK";
    }
    
    0 讨论(0)
  • 2021-01-29 16:28

    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
    
    0 讨论(0)
提交回复
热议问题