Count the instances where a variable equals TRUE

Working on a quiz website where when users submits their answers $perQuestionScore equals true when the answer is correct but false when it is wrong. I’m trying to find the instances where $perQuestionScore equals true in other to total the score but it doesn’t seem to work.My code looks like below.

<?php    

$perQuestionScore = 0;
$perQuestionScore= false;

if (isset($_POST['grader'])) {

if(isset($_POST[$chosen]))

{
        $choice= $_POST[$chosen];

    if (strpos($choice, $correctOne) !== false) {

        $perQuestionScore = true;

    echo $_POST[$chosen] . "" . " is the correct answer";

} elseif (strpos($choice, $correctOne) == false) { echo $_POST[$chosen] . "" . " is the Wrong answer";

} else  {

echo "You did not choose an answer"; { 



}
}
}               
}
     }
     echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
                    echo $perQuestionScore * 10;

}  ?>
1 Like

Hi sleek-geek welcome to the forum

You can’t use the same variable for both a boolean and a numeric at the same time.

True, PHP does “type juggling” but I don’t think that’s what you’re trying to do here.

1 Like

@Mittineague: I’m trying to calculate the total score, where each correct answer is 10 points.

I understand.

Try giving the boolean and the numeric diferent names.
And I’m a bit surprised you aren’t getting error messages if that’s all the code.
The “if” curly braces look off.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'true');

$perQuestionScore = 0;
$perQuestionScore= false;

if (isset($_POST['grader'])) {
  if(isset($_POST[$chosen])) {
    $choice= $_POST[$chosen];
    if (strpos($choice, $correctOne) !== false) {
      $perQuestionScore = true;
      echo $_POST[$chosen] . "" . " is the correct answer";
    } elseif (strpos($choice, $correctOne) == false) {
      echo $_POST[$chosen] . "" . " is the Wrong answer";
    } else  {
      echo "You did not choose an answer";
    {
    }
    }
  }               
}

}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
}
?>

No errors and that still didn’t calculate the scores!

Please post your modified code as it is currently

<?php	

error_reporting(E_ALL);
ini_set('display_errors', 'true');
							$perQuestionScore = 0;
       $perQuestionScore= false;
							 
if (isset($_POST['grader'])) {
  if(isset($_POST[$chosen])) {
    $choice= $_POST[$chosen];
    if (strpos($choice, $correctOne) !== false) {
      $perQuestionScore = true;
      echo $_POST[$chosen] . "" . " is the correct answer";
    } elseif (strpos($choice, $correctOne) == false) {
      echo $_POST[$chosen] . "" . " is the Wrong answer";
    } else  {
      echo "You did not choose an answer";
    {
    }
    }
  }               
}

}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
}



$conn->close();
?>

You still have the same variable names for two different things;

$perQuestionScore = 0;
$perQuestionScore= false;

Anytime you use an “=” it will assign that value to the variable, so it is no longer “0” after that, but “false”

Then, assuming the if conditionals are met, it will become

$perQuestionScore = true;

So whether $perQuestionScore is “false” or “true” , I don’t think PHP will know how to multiply it.

How should I handle it?

Is it possible to produce an isolated test page? Here is a start"

<?php
declare(strict_types=1); // PHP 7 ONLY

error_reporting(E_ALL);
ini_set('display_errors', 'true');

$title = 'Count the Instances';

// $perQuestionScore = 0;
$perQuestionScore = false;
               
if($DEBUG=true) {
   $_POST['grader'] = true;
   $chosen = 'maths';
}   

if (isset($_POST['grader'])) {
  if(isset($_POST[$chosen])) {
    $choice= $_POST[$chosen];
    if (strpos($choice, $correctOne) !== false) {
      $perQuestionScore = true;
      echo $_POST[$chosen] . "" . " is the correct answer";
    } elseif (strpos($choice, $correctOne) == false) {
      echo $_POST[$chosen] . "" . " is the Wrong answer";
    } else  {
      echo "You did not choose an answer";
    // {
    // }
    }
  }               
}

//}

echo $tmp = <<< TMP
<!doctype html>
<html lang="en">
<head>
<title> $title </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" >
</head>
<body>
TMP;

echo '<h1>' .$title .'</h1>';
echo '<div style="width:88%; margin:auto; border:solid 1px #ccc;">';
  echo '<form>';
    echo '<div>';  
      echo '<input id="grader" type="submit" name="grader" value="Grade Quiz" />';
    echo '</div>';  
  echo '</form>';
  echo $perQuestionScore * 10;
echo '</div>';  
echo '</body>';  
echo '</html>';  

// }

// $conn->close();

@John_Betong I run it on a local server.

@sleek-geek

Can you supply or show the input quiz source page. It should then be possible to create a simple demo and hard-code the database test results in arrays.

They are radio buttons;

Can you post your HTML code here.

1 Like

Here we go: Online Demo

2 Likes

Nice one, John!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.