PHP检查两个变量是否相同,如测试

时间:2018-10-25 13:47:40

标签: php variables

我需要帮助来检查变量(来自数据库)是否相同,例如测试。 首先,我只使用“ ==”,但是我看到了:http://php.net/manual/en/language.operators.comparison.php,现在我使用的是“ ===”。但这仍然行不通。

我的代码:

    $uids = array();
    $trues = array();
    while ($row = mysqli_fetch_assoc($result)) {
      array_push($uids, $row['UID']);
    } while ($row1 = mysqli_fetch_assoc($result1)) {
      array_push($uids, $row1['UID']);
    } while ($row2 = mysqli_fetch_assoc($result2)) {
      array_push($uids, $row2['UID']);
    }
    for ($i = 0; $i < count($uids); $i++) {
      $r = 0;
      if ($row['question1'] === $row['uAnswer1']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question2'] === $row['uAnswer2']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question3'] === $row['uAnswer3']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question4'] === $row['uAnswer4']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question5'] === $row['uAnswer5']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question6'] === $row['uAnswer6']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question7'] === $row['uAnswer7']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question8'] === $row['uAnswer8']) {
        $r++;
      } else {
        $r = $r;
      } if ($row['question9'] === $row['uAnswer9']) {
        $r++;
      } else {
          $r = $r;
      } if ($row['question10'] === $row['uAnswer10']) {
        $r++;
      } else {
        $r = $r;
      }
      array_push($trues, $r);
      echo $uids[$i] . " [" . $r . "]<br>";
    }
    print_r($trues);
  }

结果是:https://hastebin.com/uxitoyoyib.php

因此它实际上表示一切正确,但我知道事实并非如此。你能帮我吗?

谢谢!

3 个答案:

答案 0 :(得分:0)

使用

  if ($row['question4'] == $row['uAnswer4']) {
        //do something
        }

并尝试打印您的2个变量。

答案 1 :(得分:0)

比较类似变量

if ( strtolower(trim($row['question5'])) === strtolower(trim($row['uAnswer5'])) )

如果添加了空格,请删除空格,将小写的字符串转换为两个字符串的完全匹配。

答案 2 :(得分:0)

您可以使用strcmp()strcasecmp()函数或===运算符。