English 中文(简体)
PHP if or statement not working
原标题:

We are trying to use the below piece of code

if (($_GET[ 1 ] != "1") || ($_GET[ 1 ] != "2")) {

When we try this no matter what value the variable has it will evaluate as true even when data is entered that is false. When we use

if (($_GET[ 1 ] == "1") || ($_GET[ 1 ] == "2")) {

and put in data that will make it return false it works correctly. We have reversed the way that the if statement goes just so we can get this working but i would like to know why this doesnt work, if it is something im doing wrong or a limitation within php with the or and the not equal operators

Thanks

最佳回答

Your first test is saying this:

If $_GET[ 1 ] is anything other than "1" OR $_GET[ 1 ] is anything other than "2"

The expression will always pass: If it equals 1 it will pass the != 2 test on the second half of your if statement. If it equals 2 it will pass the != 1 test on the first half, and never make it to the second half of the test.

The second test simply says:

If $_GET[ 1 ] equals "1" OR $_GET[ 1 ] equals "2" then the expression should pass

You probably want this expression, which will pass only if neither parameter holds the correct value:

if(($_GET[ 1 ] !=  1 ) && ($_GET[ 1 ] !=  2 ))
问题回答

The expression below will always evaluate to TRUE, because either x is not 1 or x is not 2. There is no way that x can equal both 1 and 2 at the same time!

($x != 1) || ($x != 2)

The opposite of

($x == 1) || ($x == 2)

is

($x != 1) && ($x != 2)

Note that you have to change the || to a &&.

Try:

if (($_GET[ 1 ] != "1") && ($_GET[ 2 ] != "2")) {

If you use != in both statements, then you need to use && instead of || to make sure both statements get evaluated.

Essentially, using || means that the first statement gets evaluated and if it is false, then PHP will ignore the second statement because you are using OR. But if you use &&, then PHP will be sure to evaluate both != statements.

if your code is:

if (($_GET[ 1 ] != "1") || ($_GET[ 2 ] != "2")) {

any value or even null value can enter this condition. you can change || to && meaning any string which is not equal to "1" and "2" can enter you condition

Without knowing the intention of the statement, or what input you started with (or how you revised it), I can t say for certain what the issue is.

That said, the first statement will only evaluate to FALSE if $_GET[ 1 ] is equal to 1 and $_GET[ 2 ] is equal to 2.

The second statement will only evaluate to FALSE if $_GET[ 1 ] is not equal to 1 and $_GET[ 2 ] is not equal to 2.

I m guessing what you wanted on your first statement was

if (($_GET[ 1 ] != "1") && ($_GET[ 2 ] != "2")) {

which evaluates to FALSE unless $_GET[ 1 ] is equal to 1 and $_GET[ 2 ] is equal to 2.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签