html - Appending string with variable in PHP and looping -


i calling variables mysql database , comparing them variables being posted via html form previous page. correct answers , given answers.

my current structure looks this:

if($ans1==$que1){ echo"true"; } if($ans2==$que2){ echo"true"; } if($ans3==$que3){ echo"true"; } //and on... 

the structure not hectic until there 3 questions. questions increased 100. want know how this:

for(i=1; i=100; i++){ if($ans.$i==$que.$i){ echo"true"; $total_correct_ans=$total_correct_ans+1; } } echo"total correct answers ". $total_correct_ans; 

first off, consider using arrays instead; they're meant kind of repetitive stuff:

// $answers = [1, 3, 2]; // $questions = [1, 2, 3]; ($i = 0; $i < count($answers); ++$i) {     if ($answers[$i] == $questions[$i]) {          echo "true"      }; } 

if that's not possible, use variable variables:

if (${"ans$i"} == ${"que$i"}) { } 

Comments

Popular posts from this blog

javascript - Unusual behaviour when drawing lots of images onto a large canvas -

how can i manage url using .htaccess in php? -

javascript - Chart.js - setting tooltip z-index -