[英]undefined index but i can't understand why
Here is my code 这是我的代码
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && $_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
Please help. 请帮忙。 Thanks. 谢谢。
When you do && it will evaluate all conditions until something is false. 当您执行&&时 ,它将评估所有条件,直到出现错误为止。 When you do || 当您|| it will evaluate all conditions until something is true. 它将评估所有条件,直到某些事情成立为止。 Since your first conditions evaluated to the false, the 2nd one was invoked but $_POST['error']
didn't exist. 由于您的第一个条件评估为false,因此调用了第二个条件,但$_POST['error']
不存在。
You probably want to do this, notice the brackets around your two errors. 您可能要这样做,请注意两个错误的括号。
if(
isset($_POST['error']) &&
(
$_POST['error'] == 2 ||
$_POST['error'] == 1
)
)
It can also be better re-written as. 也可以将其更好地重写为。
if(
isset($_POST['error']) &&
in_array($_POST['error'], array(1,2))
)
Like Augwa said: 就像奥格瓦说的那样:
A solution: 一个解法:
if(isset($_POST['error'])) {
if($_POST['error'] != 2 && $_POST['error'] !=1) {
// Do stuff here
return true;
}else if($_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
}
you should change your code as below... always enclose your comparison with || 您应该按以下方式更改代码...始终将||括起来 in brackets. 括号内的。 because || 因为|| condition checks up to last piece of code to find out a 'true' value. 条件检查直到最后一段代码,以找出“真”值。 by re-coding as ..... && (... || .... ), the executions will return from the point && and will not execute ( .... || ..... ) part 通过重新编码为..... &&(... || ....),执行将从&&点返回,并且不会执行(.... || .....)部分
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && ($_POST['error'] == 2 || $_POST['error'] == 1)) {
return false;
} else {
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.