phpif-statementoperator-keywordsentence

Php Or operator doesn´works propertly


I´m trying to use or operator in a php sentence but only validate the first condition, what´s wrong?

if ($_SESSION['user_id'] != 4 || $_SESSION['rol'] != 1) 
{
//exit
}else{
  //do something
}```
 

Solution

  • You should invert the condition in this way:

    if ($_SESSION['user_id'] == 4 || $_SESSION['rol'] == 1) 
      //it's ok
    else
      //exit
    

    Because otherwise if the user_id is 4 it will otherwise fail if it doesn't have the role expected