【问题标题】:How to test the index of an array for equality with another index of an array in php?如何测试数组的索引与php中数组的另一个索引是否相等?
【发布时间】:2014-04-20 02:16:19
【问题描述】:

我想测试一个数组的特定索引是否等于另一个数组中的另一个特定索引,但即使两个索引相等,它总是为假。

<!DOCTYPE html>
<html>
<head><title>PHP Written Test</title></head>
<body>
<?php include 'variables.php';?>
<?php include 'functions.php';?>
<?php

session_start();
$x = 1;
$y = 0;

while ($x <= 20)
{   
    $a = $_POST["question" . $x];
    $b = $_SESSION['correct'][$y];

    if ($a == $b)
    {
        echo "The answer was correct " . "<br><br>";
    }
    else
    {
        echo "The answer was wrong<br><br>";
    }
    echo "The user answer is: " . $a . "<br>";
    echo "The correct answer is: " . $b  . "<br><br>";
    $x++;
    $y++;
}
?>
</body>
</html> 

它输出总是错误的我不明白为什么这不起作用

The answer was wrong

The user answer is: Software programs that can compose, send, and receive email messages
The correct answer is: A service that allows users to send messages and/or documents to each other over an internet network

The answer was wrong

The user answer is: Click the refresh button
The correct answer is: Click the refresh button

The answer was wrong

The user answer is: A copy of the email sent to the recipient emailed to you
The correct answer is: A copy of the email sent to the recipient emailed to you

The answer was wrong

The user answer is: Software enabling the user to organize emails
The correct answer is: A tool used to lookup information on the internet

The answer was wrong

The user answer is: A location that stores saved files and programs
The correct answer is: A location that stores saved files and programs

The answer was wrong

The user answer is: A program that lets your organize data into databases
The correct answer is: A utility program used for managing processes and programs running on the computer

The answer was wrong

The user answer is: A program that infects a computer, copies itself many times using up system resources, and spreads the infection to other computers
The correct answer is: An important part of systems files. It manages the interaction between the user, application programs, and hardware

The answer was wrong

The user answer is: GPU
The correct answer is: CPU

The answer was wrong

The user answer is: Icons
The correct answer is: Icons

The answer was wrong

The user answer is: Composing a new email message and sending it
The correct answer is: Impersonating someone with a similar email address and trying to obtain sensitive information from the recipient

The answer was wrong

The user answer is: Menu Bar
The correct answer is: Address Bar

The answer was wrong

The user answer is: The home page
The correct answer is: The home page

The answer was wrong

The user answer is: Motherboard
The correct answer is: Keyboard

The answer was wrong

The user answer is: Programs that intend to damage a users data and system files or allow an outside attacker to gain access to the computer and steal data
The correct answer is: Programs that intend to damage a users data and system files or allow an outside attacker to gain access to the computer and steal data

The answer was wrong

The user answer is: The message itself
The correct answer is: The 'To' line

The answer was wrong

The user answer is: Desktop
The correct answer is: Computer

The answer was wrong

The user answer is: Internet Explorer
The correct answer is: Internet Explorer

The answer was wrong

The user answer is: User ID, @ symbol, host name
The correct answer is: User ID, @ symbol, host name

The answer was wrong

The user answer is: Explorer
The correct answer is: Explorer

The answer was wrong

The user answer is: Shortcut
The correct answer is: Taskbar

【问题讨论】:

  • 尝试if (trim($a) == trim($b))检查答案或正确答案中是否有一些随机的前导/尾随空格,导致if失败。
  • 你能不能 var_dump($_SESSION)。

标签: php arrays indexing equality


【解决方案1】:

我按照 Sean 的建议做了并使用了 if (trim($a) == trim($b)) 它解决了我的问题。

【讨论】:

    【解决方案2】:

    但即使两个索引相等,它总是为假

    那么这两个索引不相等,要么是因为它们 不同,要么是你的脚本错误(即没有正确检查某些内容)。

    如果 PHP 声明某些内容为 FALSE,那么它就是 FALSE,即使由于 PHP 的工作方式(例如 == 和 === 之间的区别)而对您来说是模糊或未知的。

    我无法直接回答您,因为我看不到您的会话保存的原始数据,但可以帮助您排除故障(这个和未来的脚本):

    我看到你的例子:

    用户回答是:点击刷新按钮
    正确答案是:点击刷新按钮

    它们看起来一样,但 PHP 不会将它们解释为相同。
    一个数据来自 DB,另一个数据来自用户输入 - 例如,它们是否相同?

    检查源代码并比较两者。突出显示文本,某处可能有胭脂空间。

    var_dump() 您的会话并比较数据。

    在您的循环中手动设置$a$b(而不是会话等),将它们设置为相同的字符串(即它们匹配),看看您是否得到TRUE,即20“答案是真的”。

    如果是这样,您的脚本、循环和 IF 正在工作到会话数据的位置。
    然后仔细检查会话数据。
    检查源代码并再次比较两者。

    回显所有内容,检查 PHP 实际上 有什么数据,而不是它最终输出到屏幕上的数据,而不是你认为它拥有的数据。

    有一些不同的东西,你只需要找到它。

    【讨论】:

    • 我刚做了这个,它起作用了 if (trim($a) == trim($b)) var_dump() 的目的到底是什么......它有什么作用?跨度>
    • @DaveCribbs 太棒了!很高兴你被排序。由于我们看不到原始数据,我只想为您提供一些基本故障排除的指导。请记住,添加 trim() 可能会解决 this 问题,但也可能会介绍其他问题! trim() 去除各种东西,其中一些你可能想要留下。相应地处理数据,因为你可能会改变它(即使用修剪)但后来不希望它处于那种状态(即你打印出来给用户,现在没有空格)php.net/manual/en/function.trim.php
    • var_dump() "显示有关一个或多个表达式的结构化信息,包括其类型和值。数组和对象通过缩进显示结构的值递归探索。"试一试,自己看看。像所有代码一样,它在正确的场景中有它的用途。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2021-04-17
    • 2015-08-14
    相关资源
    最近更新 更多