Search code examples
phpcomparison-operators

PHP if else ignores after "OR/||" Operator


Hi I'm having a problem with this line of code.

if($_SESSION['permission'] !=  "superadmin" || "admin"){
    //header("location:logout.php");
    echo $_SESSION['permission'];
}

It is a quick line that would logout the user if the access level is not superadmin or admin but my if else is ignoring the "admin" after the || operator. Would appreciate any help on what could I be doing wrong. Thanks. Also tried && and it still doesn't work.


Solution

  • just fix your if to the following :

    if($_SESSION['permission'] !=  "superadmin" &&  $_SESSION['permission'] != "admin"){
    

    in your code you have two conditions

    $_SESSION['permission'] !=  "superadmin" || "admin"
    

    and this is interpreted like :

    $_SESSION['permission'] !=  "superadmin"
    OR 
    "admin"
    

    and the condition

     "admin"
    

    is equvilant to true because its none empty value so it will always pass the if statment