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

<!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

===============>>#1 票数:0 已采纳

我做了肖恩建议和使用的if(trim($ a)== trim($ b))它解决了我的问题。

===============>>#2 票数:0

但即使两个指数相等,它也总是假的

然后,这两个指标是不相等的,要么是因为它们不同的,或者你的脚本是错误的(即不正确地检查东西)。

如果PHP声明某些内容为FALSE,那么它就是FALSE,即使由于PHP的工作方式(例如==和===之间的差异),它也是一些模糊或未知的东西。

我不能直接给你答案,因为我看不到你的会话所持有的原始数据,而是帮助你排除故障(这个和将来的脚本):

我通过你的例子看到你:

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

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

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

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

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

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

回应所有内容,检查PHP 实际拥有的数据,而不是它最终在屏幕上输出的内容,而不是您认为它具有的内容。

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

  ask by Dave Cribbs translate from so

未解决问题?本站智能推荐:

6回复

php中的==和=== WRT数组之间的区别?

我正在阅读关于php的内容,它说, ==是Equality,如果$a和$b具有相同的元素,则$a == $b为true 。 ===是Identity,如果$a和$b具有相同的元素,相同的类型,以相同的顺序,则$a === $b为true 。 所以,我想我会尝试看看自己的
3回复

未定义的偏移量和数组比较PHP

嘿,每个人都完成了我的研究,我发现即时消息仍然卡住了很多人,说要在变量前加上@符号,但它似乎没有用,所以我的代码给了我这个错误 我的代码是 所以它所指的线是 香港专业教育学院尝试了许多其他变体,例如删除$ lineArray前面加@的单引号,并同时执行这两个操作,有人可以
1回复

php数组索引的目标是与另一个数组同名的索引?

嗨,我知道这对您来说可能很容易,但是我刚刚开始对php进行一些基础研究,我想问一下获取数组第二个索引的可能方法是什么,例如:假设我们有2个数组: 有没有可能仅从第二个数组来定位索引“名称”的方法?
19回复

如何在PHP中为数组重新索引?

我有以下数组,我想对其重新索引,以使键反向(理想情况下从1开始): 当前数组( 编辑:该数组实际上看起来像这样): 应该如何:
5回复

如何在php数组及其索引中获得前3个值

我想获得最高价值,第二高价值和第三高价值 例如,我有一个像这样的数组: $n = array(100,90,150,200,199,155,15,186); 我知道获取最大值及其索引的方法: echo max($n); //200 $maxs = array_keys($n
3回复

PHP:如何在未索引的数组中搜索?

我有一个这样格式化的数组(此示例有5个键): 阵列键有时会交替显示,因为可以省略。 如何搜索其中带有“ Server:...”的数组,如果存在则显示它? 对于我的一生,我感到困惑!
4回复

如何在php中重新索引数组? [重复]

这个问题已经在这里有了答案: 如何在PHP中为数组重新索引? 19个答案 我有一个像这样的数组: 如何更改第一级的索引(仅第一级,没有递归),以使索引为[0],[1],[2]而不是[0],[2],[3],但仍保持订购?
1回复

按索引对PHP中的数组进行排序

我有以下数组: 我如何才能先按“ nbr1”降序排序,然后按“ nbr2”降序排序此数组? 我尝试了在网络上解释过的不同方法,但是没有一种方法适用于这种情况。 谢谢
2回复

如何在PHP中创建返回最后一个数组索引的静态变量?

我想创建一个静态变量的函数,因为我需要保留函数之间的变量值。 第一种方法,此变量​​包括数组的最后一个索引,第二次运行时将更改该值。 如果我使用count()搜索最后一个数组索引,则会收到以下错误:解析错误:解析错误,期望在C:\\ wamp \\ www \\ rekurzio \\ in
1回复

字符串到数组-仅具有一个索引的数组(PHP)

我有这个字符串: 它应该成为一个数组: 但我得到: 我使用循环来获取此字符串: 接下来,我使用explode函数获取数组。 为什么我只有一个索引-零的数组? 我应该做什么: 请帮助我理解这一点。