PHP if ||

I want to use an if to check that a var is not equal to either of two values but can not get the or to work


if ($val != "a") || ($val != "b") {
echo "you did not enter a or b";
}

Open in new window

lvmllcAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xtermCommented:
You need parentheses around the entire expression
if ( ($val != "a") || ($val != "b") ) {
        echo "you did not enter a or b";
}

Open in new window

ukerandiCommented:

if ((strcmp($val,"a")!=0) || (strcmp($val,"b")!=0))
{

echo "you did not enter a or b";
}
ukerandiCommented:
$value="";

if  ($val != "a") {
        $value="a";
}
if ($val != "b")
{
$value="b";
}

if ($value="")
{
echo "you did not enter a or b";

}
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

StingRaYCommented:
1. You forget to put outer parentheses.
2. You should use && instead of ||.

if (($val != "a") && ($val != "b")) {
echo "you did not enter a or b";
}

Open in new window

sharkodlakCommented:
You have an error in parentheses. Omit inner parentheses around logical expression. Also change expression to AND, which is what you need.
if ($val != "a" || $val != "b") {
  echo "you did not enter a or b";
}

Open in new window

lvmllcAuthor Commented:
Thanks to everyone for the responses  - however it seems that this still does not work as I would expect.

What I am looking for is the ability to have a single if statement  that checks to see if a value is != to either of two values.

I tried out all of the ideas submitted and only  attempt 3 and 4 worked.
The question I have about attempt four is why does it take each one ant test independently - I would have though that the && would require it to meet both criteria as opposed to a single criteria?




$val = "a";

print "value = ".$val.".<br>";




// attempt 1
if ( ($val != "a") || ($val != "b") ) {
        echo "1. You did not enter a or b.<br>";
}




// attempt 2
if ((strcmp($val,"a")!=0) || (strcmp($val,"b")!=0))
{

echo "2 You did not enter a or b. <br><br>";
}






// attempt 3
$value="";
if  ($val != "a") {
	$value="a";
}
if ($val != "b")
{
	$value="b";
}
if ($value="")
{
echo "3 You did not enter a or b. <br><br>";
}



// attempt 4
if (($val != "a") && ($val != "b")) {
echo "4. You did not enter a or b. <br><br>";
}


	
// attempt 5
if ($val != "a" || $val != "b") {
  echo "5 You did not enter a or b. <br><br>";
}

// attempt 6
if ($val != "a" && $val != "b") {
  echo "6 You did not enter a or b. <br><br>";
}

Open in new window

StingRaYCommented:
I am not sure if I understand your question correctly. You said you are checking if a value is not either 'a' OR 'b'. This can be translated to programming language as "AND" or "&&".
xtermCommented:
You're applying the "either" part in the wrong place in your mind :)

You're saying that you if either $a or $b is not equal to $val, then neither $a nor $b was entered, which seems like an OR, but is not.

Restating it in its reality says "Neither $a nor $b was entered if $a is not equal to $val AND (not 'or') $b is not equal to $val".

That is the reason for the confusion, and that is why my (very first you received) comment works as you intend, and the others do not.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
liveaspankajCommented:
&& means AND and != mean NOT

so your attempt 4 says
($val != "a") && ($val != "b")

(Val is NOT a) AND (Val is also NOT b) i.e Val is neither a nor b

if you try attempt 1:
( ($val != "a") || ($val != "b") )
(Val is NOT a) OR (Val is NOT b) i.e Val is either not a or it is not b, that would mean, one of the condition would always meet. and you will always get a true condition

:) So thats plain simple
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.