Php If Statment with "or" clause

Experts, I'm having a syntax issue.

This code works... it checks to see if the logged in user is an admin.  If they are not an admin, it redirects the user correctly...

if ($row['admin_id'] !== $_SESSION['UserID'])
{
	header("Location: noaccess.php"); /* Redirect browser */
	exit;
}

Open in new window


I now want to modify that code to also allow a superuser access, but I'm having an issue with the OR statement...  Let's say the Superuser's ID was 100.

I've tried the following examples with no luck:

if ($row['admin_id'] !== $_SESSION['UserID'] OR '100')
{
	header("Location: noaccess.php"); /* Redirect browser */
	exit;
}

Open in new window



if ($row['admin_id'] !== $_SESSION['UserID'] || $row['admin_id'] !== '100')
{
	header("Location: noaccess.php"); /* Redirect browser */
	exit;
}

Open in new window


Any suggestions?

-dsg
dsg138Asked:
Who is Participating?
 
GaryConnect With a Mentor Commented:
Shouldn't it be &&, OR will always return true if one of the condition is not met.

if ($row['admin_id'] != $_SESSION['UserID'] && $row['admin_id'] != '100')
{
      header("Location: noaccess.php"); /* Redirect browser */
      exit;
}

Though if there is only one admin id why would you need two checks.
0
 
Marco GasiFreelancerCommented:
Your first code is wrong: OR is not inm php syntax. For the second one, try this:

if ($row['admin_id'] != $_SESSION['UserID'] || $row['admin_id'] != '100')
{
      header("Location: noaccess.php"); /* Redirect browser */
      exit;
}
                                 
Cheers
0
 
Marco GasiConnect With a Mentor FreelancerCommented:
Oh, yes, GaryC123 is right.
But note GaryC123 has used != and not !==: this is because if you use !== or === notation, you're checking not only the value but the type also. So, if $row['admin_id'] is an integer value and you check to see if it is !== '100', you'll ever get a false result since '100' is a string.

Read more about comparison operators here: http://it2.php.net/manual/en/language.operators.comparison.php

Cheers
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Marco GasiFreelancerCommented:
A last note: you should use OR clause only if you would check if values are equals:

if ($row['admin_id'] == $_SESSION['UserID'] || $row['admin_id'] == '100')
{
//some stuff as
echo "Wellcome, $row['admin_id']";
}else{
      header("Location: noaccess.php"); /* Redirect browser */
      exit;
}
0
 
GaryCommented:
Actually you can use OR or ||
http://www.w3schools.com/php/php_operators.asp
0
 
Marco GasiFreelancerCommented:
This is wath I read in that page

Operator: x || y       
Name: Or
Description:       True if either or both x and y are true
Examples: x=6
                 y=3
                 (x==5 || y==5) returns false

I don't see anything which could make think you cvan use OR in a php code

Cheers
0
 
Marco GasiFreelancerCommented:
I saw it now :-)
0
 
dsg138Author Commented:
Thanks Guys,
Gary, you're correct, it should've been &&.  By using an Or clause, I was almost guaranteeing it returned false every time because in almost every situation, one of those id's will be false.

Marqus, Thanks, I was not aware of the difference between != and !==.  My id is always an integer so I used '!=100'.  I suppose !==100 would work if I didn't use quotes?

Appreciate the help!
-dsg
0
 
GaryCommented:
Yes, because !== compares the value and var type (e.g. are they both a string or are they both a number) while != only compares the value regardless of its type (number or string doesn't matter)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.