为什么如果isset与语法匹配不起作用

时间:2017-07-04 08:10:16

标签: php html debugging

我希望ifset $_POST["items"]文本与reject1文本匹配,然后回显。我的代码不起作用为什么?

if(isset($_POST["items"])==='reject1'){
   echo 'text match :)';
}
else {
   echo 'not match :(';
}

7 个答案:

答案 0 :(得分:1)

试试这个

if(isset($_POST["items"]) && $_POST["items"]==='reject1'){
   echo 'text match :)';
}
else {
   echo 'not match :(';
}

它适用于你。

答案 1 :(得分:1)

你必须这样写:

if(isset($_POST["items"]) && $_POST["items"]==='reject1'){
   echo 'text match :)';
}
else {
   echo 'not match :(';
}

因为isset仅返回 true false ,而不是值。

答案 2 :(得分:1)

你应该使用内部if作为字符串比较

if(isset($_POST["items"])){
  if ($_POST["items"] ==='reject1'){
    echo 'text match :)';
  } else {
   echo 'not match :(';
  } 
}

答案 3 :(得分:0)

php isset()返回true或false,因此无法匹配“拒绝”'。如果你想匹配,你应该这样做:

if($_POST["items"]==='reject1') {}

或者如果你想检查变量是否存在,你可以这样做:

if(isset($_POST["items"]) && $_POST["items"]==="reject1") {
echo "text match";} else { echo "not match"; }

答案 4 :(得分:0)

如果设置了变量,

isset会返回TRUE,如果不是,则返回FALSETRUEFALSE都不等于'reject1',因此永远不会输入if

您可以执行两阶段检查 - 首先检查密钥是否已设置,然后评估其值:

if(array_key_exists('items', $_POST) && $_POST['items'] === 'reject1') {
   echo 'text match :)';
} else {
   echo 'not match :(';
}

答案 5 :(得分:0)

使用PHP 7 null coalesce运算符,您可以执行类似

的操作
if ( $_POST["items"]??null === 'reject1' )  {

答案 6 :(得分:0)

或做一个班轮:

echo (isset($_POST["items"]) && $_POST["items"]==='reject1')? 'text match :)' : 'not match :('; 

参考:http://php.net/manual/ro/function.isset.php

  

如果var存在且值不是NULL,则返回TRUE。否则就错了。