Possible Duplicate:
Compare multiple values in PHP
Will someone help me to find a better way of writing this, please. I feel as if there has to be something better than nested ifs. I have tried searching, but I don’t know what I am looking for.
The code says that if the session variable “auth” is not exactly equal to 0, 1, 2, or 3 then redirect to the login page.
if($_SESSION['auth']!=0){
if($_SESSION['auth']!=1){
if($_SESSION['auth']!=2){
if($_SESSION['auth']!=3){
header("location:login.php");
}
}
}
}
My initial thought was to do something like this.
if($_SESSION['auth']<=0 || $_SESSION['auth']>=3){
header("location:login.php");
}
But that doesn’t account for fractions of the integer value. Which I only want to allow integer values. My next thought was this.
if($_SESSION['auth']!=0 && $_SESSION['auth']!=1 && $_SESSION['auth']!=2 && $_SESSION['auth']!=3){
header("location:login.php")
}
But this way is not much different than all the nested ifs. Let me know if I am just attempting to fix something that isn’t broken. Thank you in advance. A link to the relevant topic (PHP manual preferably) is all I’m looking for, not the solution.
Yes, you are. A simpler approach could be inarray().
or even shorter
It is probably best to go with the first option, just in case you may want to compare it against more values in the future.