Frage

I would like to test if a specific index of an array is equal to another specific index in another array but it always is false even if the two indexes are equal.

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

It outputs that it is always wrong I don't understand why this isn't working

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
War es hilfreich?

Lösung

I did what Sean suggested and used if (trim($a) == trim($b)) It fixed my issue.

Andere Tipps

but it always is false even if the two index's are equal

Then the two indexes are not equal, either because they are different, or your script is wrong (ie not checking something correctly).

If PHP states something is FALSE, then it IS FALSE, even if it's something obscure or unknown to you due to the way PHP works (for example the difference between == and ===).

I cannot give you the direct answer as I cannot see the raw data your sessions hold, but to help you troubleshoot (this and future scripts):

I see by your examples you have:

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

They look the same, but PHP is not interpreting them as the same.
Is one data from DB and another from user input - eg are they the same?

Check the source code and compare the two. Highlight the text, there may be a rouge space somewhere.

var_dump() your session and compare the datas.

Set $a and $b manually in your loop (instead of sessions etc), to both the same string (ie they match), and see if you get TRUE, that is 20 "The answer is true".

If so, your script, loop and IF is working up to the point of the session data.
Then check the session data carefully.
Check the source code and compare the two again.

Echo everything out, check what data PHP actually has, not what it ends up outputting on your screen, and never what you think it has.

There IS something different, you just have to find it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top