Note that there are some explanatory texts on larger screens.

plurals
  1. PORead questions and answers in an array and display
    text
    copied!<p>Comparing array elements and add 1 or 0 if the arrays are the same or not. But they are not adding up, where could i be doing it wrong? Because the arrays do not seem to be comparing.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Chosen answers&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;pre&gt; &lt;?php //Posting of the chosen answers $answers = $_POST['selected_answers']; echo '&lt;b&gt;&lt;u&gt;THE ANSWERS YOU HAVE CHOSEN ARE:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;'; print_r($answers); //Opening of the answers file, reading and printing it $openFile = fopen("answers.txt", "r") or exit ("unable to open the answers file"); $fileContents = fread($openFile, filesize("answers.txt")); fclose($openFile); $delimiter = " "; $myArray = explode($delimiter, $fileContents); $score = $score1 = $score2 = $score3 = $score4 = $score5 = $score6 = $score7 = $score8 = 0; //Computation of marks scored for the answered questions if ($answers[0] == $myArray[0]) { $score = 1; } elseif ($answers[0] !=$myArray[0]) { $score = 0; }echo '&lt;br /&gt;'; if ($answers[1] == $myArray[1]) { $score1 = 1; } elseif ($answers[1] !=$myArray[1]) { $score1 = 0; }echo '&lt;br /&gt;'; if ($answers[2] == $myArray[2]) { $score2 = 1; } elseif ($answers[2] !=$myArray[2]) { $score2 = 0; }echo '&lt;br /&gt;'; if ($answers[3] == $myArray[3]) { $score3 = 1; } elseif ($answers[3] !=$myArray[3]) { $score3 = 0; }echo '&lt;br /&gt;'; if ($answers[4] == $myArray[4]) { $score4 = 1; } elseif ($answers[4] !=$myArray[4]) { $score4 = 0; }echo '&lt;br /&gt;'; if ($answers[5] == $myArray[5]) { $score5 = 1; } elseif ($answers[5] !=$myArray[5]) { $score5 = 0; }echo '&lt;br /&gt;'; if ($answers[6] == $myArray[6]) { $score6 = 1; } elseif ($answers[6] !=$myArray[6]) { $score6 = 0; }echo '&lt;br /&gt;'; if ($answers[7] == $myArray[7]) { $score7 = 1; } elseif ($answers[7] !=$myArray[7]) { $score7 = 0; }echo '&lt;br /&gt;'; if ($answers[8] == $myArray[8]) { $score8 = 1; } elseif ($answers[8] !=$myArray[8]) { $score8 = 0; } $Total = $score + $score1 + $score2 + $score3 + $score4 + $score5 + $score6 + $score7 + $score8 ; echo "&lt;b&gt;&lt;u&gt;$Total&lt;/u&gt;&lt;/b&gt;"; ?&gt; &lt;/pre&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>How can I be able to compare the two arrays and compute the total marks. the first array $answers is from a submitted form. and the second one is read from another text file called answers.txt</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload