Problem With multiple != statements

I need to check if one of several values is not equal to some specified value. For example:
[php]
if($var != 1 || $var != 2 || $var != 3) {
echo ‘pass’;
} else {
echo ‘fail’;
}
[/php]

In such a case, I would expect it to show “pass” on the page if $var was not equal to 1, 2, or 3. If it were equal to any of those, I would expect it to show “fail” instead. What actually happens is it always says “pass”.

I have tried the following and it does what I want:
[php]
if($var != 1 && $var != 2) {
echo ‘pass’;
} else {
echo ‘fail’;
}
[/php]
However, it does not continue in this behavious if I add more than two items for it to test. Does anyone have any ideas on what I am doing wrong?

i dont quite understand what you wanted but i gave it a try

[php]

<?php if(($var != 1) || ($var != 2) || ($var != 3)) { echo 'pass'; } else { echo 'fail'; } ?>

[/php]

if variable var is equal to 1,2 or 3 it will fail else it will pass

Here is my original code:
[php]
if (($registrantArraySession01[$i] != 1) || ($registrantArraySession01[$i] != 2) || ($registrantArraySession01[$i] != 0)) {
$allowRegistration = 0;
echo ’ - Please select a value’;
}
[/php]
No matter what value is selected, it sets $allowRegistration to 0 and displays that error message. This is part of a form validation system. I am using PHP 5.2.17

are you running a loop ? otherwise $i will always be the same value

Yes, sorry I forgot to mention that part. It is trying to see if values from a dropdown have been selected. I need to check values for multiple possible registrants who are registering for a conference so each iteration of the loop is for another registrants data.

I have come up with a function that does what I need it to do. I still don’t understand why that if statement did not work properly though.

$registrantArraySession01 hold the slectedIndex for selected values?

If that so, and you want to get all the selected values from miltple select dropdown you can run a simple for each loop and will gets you all the selected valuees which you hold in an array

I am already able to get most data out of the array. This is something the check that the data that is posted is indeed valid and prevent people from sending manipulated post data. I think that the fact that it is an array is irrelevant because even this does not do what is expected:
[php]<?php
$val = 1;
if(($val != 0) || ($val != 2) || ($val != 1)) {
echo ‘pass’;
} else {
echo ‘fail’;
}
?>[/php]
I would expect that to echo pass but instead it echos fail.

well i don’t think that is the best way to check against manipulated post data.

you can get the values selected normally as you would do with any other post, then compare selected value to your predefined values.

Sponsor our Newsletter | Privacy Policy | Terms of Service