Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

PHP basic question about if statements and echo.?

Hey all I have a basic question. I want to have an answer page (script below) and it only it display "correct" if the person has the correct answer. My if statement with echo is not working and i cannot figure out whats wrong with it. Please tell me how to fix it.

<?php

// Set up arrays of things we want to tell people about this answer

$title[1] = "Blue";

$title[2] = "Green";

$title[3] = "Red";

$title[4] = "Purple";

$title[5] = "Orange";

$page_title = "Quiz Answer: You Are " . $title[$_GET['q1']];

$correctanswer = "c";

$answer_display = "The correct answer is 'C'";

$useranswer = $_GET['q1'];

?>

<HTML>

<HEAD>

<TITLE>

<? echo $page_title; ?>

</TITLE>

</HEAD>

<BODY>

<!--

<? echo $answer_display; ?>

//-->

<?php

if ($useranswer = $correctanswer)

{ echo "Correct!";

}

?>

<? echo $useranswer?>

<? echo correctanswer?>

<? echo htmlspecialchars($answer_display); ?>

</BODY>

</HTML>

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    Change this:

    if ($useranswer = $correctanswer)

    To this:

    if ($useranswer == $correctanswer)

    In PHP (and every other language derived from C), a single = means assignment, == means test for equality.

    Your if statement assigns the value $correctanswer to $useranswer, then evaluates the value assigned to determine if it is true or false.

  • 9 years ago

    Just briefly scanning over your code when I noticed your if statement has a single equal sign. If you want to compare that the two variables are equal in PHP, you need two equal signs like this:

    if($useranswer == $correctanswer)

  • nimmo
    Lv 4
    4 years ago

    i could propose no longer exiting and entering into very own living house page so freely. It does take some processing skill to accomplish that. the controversy is open approximately whether this is rather worth it, yet i hit upon it to be lots much less demanding to stay in very own living house page for as long as feasible. as quickly as you are trying this, you're able to desire to be lots extra waiting to come to a decision the subject. i'm a professional and that i could desire to troubleshoot each and all the time, and preserving one coding convention in the process the whole website is a ways much less demanding for me. terrific of excellent fortune!

Still have questions? Get your answers by asking now.