Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:42:26+00:00 2026-06-13T04:42:26+00:00

Possible Duplicate: Compare multiple values in PHP Will someone help me to find a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:42:27+00:00Added an answer on June 13, 2026 at 4:42 am

    Yes, you are. A simpler approach could be inarray().

    $values = array('0','1','2','3');
    if(!in_array($_SESSION['auth'], $values, true)
    //do something
    

    or even shorter

    if(!in_array($_SESSION['auth'], array('0','1','2','3'), true)
    //do something
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Compare multiple values in PHP I want to do something like this
Possible Duplicate: A better way to compare Strings which could be null I have
Possible Duplicate: php date compare PHP compare dates I have this code which permits
Possible Duplicate: How to compare Dates in C# This code of mine: public static
Possible Duplicate: How to compare two dates to find time difference in SQL Server
Possible Duplicate: How to compare strings I want to Compare to registry string values
Possible Duplicate: Precision of Floating Point Can you compare floating point values exactly to
Possible Duplicate: php date compare I have a date that I take from the
Possible Duplicate: How do I compare strings in Java? I'm sorry for this rather
Possible Duplicate: What is the best way to compare two entity framework entities? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.