PHP如果或声明不起作用 [英] PHP if or statement not working

查看:106
本文介绍了PHP如果或声明不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用以下代码

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")) {

并输入将使其返回false的数据它工作正常。我们已经改变了if语句的方式,所以我们可以使这个工作,但我想知道为什么这不起作用,如果它是我做错了或在php中与or和不等运算符的限制

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

谢谢

推荐答案

你的第一个测试就是这样说:

Your first test is saying this:


如果 $ _ GET ['1'] 以外的任何其他内容1 $ _ GET ['1'] 不是2

表达式将始终通过:如果它等于 1 ,它将通过!='2'测试你的if语句的后半部分。如果等于 2 ,它将在前半部分通过!='1'测试,并且永远不会进入测试的后半部分。

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.

第二个测试只是说:


如果 $ _ GET ['1'] 等于1 $ _ GET ['1 '] 等于2然后表达式应该传递

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'))

这篇关于PHP如果或声明不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆