How could I make this code more efficient? Instead of using many if-statements, should I make a function, array, or switch? How would I do that?

<form method="post" action="trivia2.php">
<li>How many days are in a week? <input type="text" name="daysweek"></li>
<li>How many days are in a year? <input type="text" name="daysyear"></li>
<li>How many weeks are in a year? <input type="text" name="weeksyear"></li>
<li>What color is the sky? <input type="text" name="colorsky"></li>
<li>what is us president name? <input type="text" name="presidentname"></li>
<li>How many sides does a triangle have? <input type="text" name="sidestriange"></li>
<li>What is the first element on the periodic table of elements? <input type="text" name="firstelement"></li>
<li>Bees create what sweet substance? <input type="text" name="beesubstance"></li>
<li>How many stripes are displayed on the American flag?<input type="text" name="stripes"></li>
<input type="submit" value="Done!"/>
echo "1. you got the right answer: $daysweek<br/>";
}else {
    echo "number 1 is wrong<br/>";}

echo "2. you got the right answer: $daysyear<br/>";
}else {
    echo "number 2 is wrong<br/>";}

echo "3. you got the right answer: $weeksyear<br/>";
}else {
    echo "number 3 is wrong<br/>";}

echo "4. you got the right answer: $colorsky<br/>";
}else {
    echo "number 4 is wrong<br/>";}

echo "5. you got the right answer: $presidentname</br>";
}else {
    echo "number 5 is wrong<br/>";}

echo "6. you got the right answer: $sidestriangle</br>";
}else {
    echo "number 6 is wrong<br/>";}

echo "7. you got the right answer: $firstelement<br/>";
}else {
    echo "number 7 is wrong<br/>";}

echo "8. you got the right answer: $beesubstance<br/>";
}else {
    echo "number 8 is wrong<br/>";}

echo "9. you got the right answer: $stripes<br/>";
}else {
    echo "number 9 is wrong<br/>";}
echo "<h3>You have $wrong wrong answers</h3>";
  • \$\begingroup\$ Security should be a far greater concern here. \$\endgroup\$ Aug 21, 2013 at 2:19
  • \$\begingroup\$ The answer to #2 is incorrect on leap years, but I digress. \$\endgroup\$
    – icktoofay
    Aug 21, 2013 at 4:11

2 Answers 2


Theres nothing particularly unefficient about what you have, but there are a couple of things. instead of storing the variables like


just leave them in $_POST like


and then instead of

echo "1. you got the right answer: $daysweek<br/>";

you already know the answer is 7 so just

echo "1. you got the right answer: 7<br/>";

but based on the content of your application you are not yet in a place where you need to be worrying about optimization

  • 1
    \$\begingroup\$ I think what Seth gave is nearly as far as it gets to save some resources, on the other hand it is a bad habit to use a variable if you are not 100% sure if it exists. \$\endgroup\$
    – Daniel P
    Aug 20, 2013 at 3:48
  • \$\begingroup\$ if $_POST['daysweek'] does not exist, neither will $daysweek=$_POST['daysweek'] \$\endgroup\$
    – Seth Cohen
    Aug 20, 2013 at 3:51

How about this.. To add or remove answers, all you would have to do is change the $answers array making the code easy to maintain.

$answers = [
            "daysweek" => 365,
            "daysyear" => 365

$answerNumber = 1;
$wrong = 0;

foreach($answers as $key => $value){

    echo "$answerNumber. you got the right answer: $value<br/>";

  } else {




function checkAnswer($key){
  return $_POST[$key] == $answers[$key] ? TRUE : FALSE;
  • \$\begingroup\$ can u please explain me this line: return $_POST[$key] == $answers[$key] ? TRUE : FALSE; what does ? True: FALSE mean? \$\endgroup\$
    – Ham Dlink
    Aug 20, 2013 at 4:15
  • \$\begingroup\$ @HamDlink ? : is a conditional operator more or like if else. This is same as writing if($_POST[$key] == $answers[$key]) { echo "TRUE"; } else { echo "FALSE"; } , the former one saves a lot of code writing but both serve the same purpose. \$\endgroup\$
    – Shankar Damodaran
    Aug 20, 2013 at 8:25
  • 2
    \$\begingroup\$ Why not remove the ? TRUE : FALSE completely? == returns a boolean anyway. \$\endgroup\$
    – icktoofay
    Aug 21, 2013 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.