Variable does not increment in PHP script
<p>First: This is not a repeat of <a href="http://stackoverflow.com/questions/8042681/deprecated-session-is-register-error-php">this question</a>.  Same script, but different question.</p>

<p>I had initially thought the error in my previous question would have solved this error, but it didn't.</p>

<p>I'm making a simple 3-question quiz via PHP. The quiz runs without syntax errors, however the problem is that my variable $correct does not seem to increment throughout the quiz (given a correct answer).  I put print statements to see if it changes but nothing gets printed.  Somehow its not properly incrementing the value of the variable. </p>

<p>Instead of outputting:</p>

<pre><code> You got 2 out of 3 correct. //where 2 is the value of $correct
</code></pre>

<p>It outputs:</p>

<pre><code>You got correct out of correct. //where the value $correct seems to have no value
</code></pre>

<p>What am I doing wrong?</p>

<pre><code> &lt;?php
 ini_set('session.gc_maxlifetime',900);
//echo ini_get("session.gc_maxlifetime"); 
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

 if(isset($_SESSION["blah"]))
{
$_SESSION["number"] = 0;
$_SESSION["correct"] = 0;
}

$total_number = 3;

print &lt;&lt;&lt;TOP
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; History Quiz &lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt; History Quiz &lt;/h3&gt;
TOP;

$number = $_SESSION["number"];
$correct = $_SESSION["correct"];


if ($number == 0){
print &lt;&lt;&lt;FIRST
&lt;p&gt; You will be given $total_number questions in this quiz. &lt;br /&gt;&lt;br/&gt;
  You will have 15 minutes to complete it. &lt;br /&gt;&lt;br/&gt;
  You cannot go back to change previous answers.&lt;br /&gt;&lt;br/&gt;
  Here is your first question: &lt;br /&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;1. Abe Lincoln was born in Illinois.&lt;/p&gt;
&lt;p&gt;
    &lt;label&gt;&lt;input type="radio" name="question1" value="true" /&gt; True &lt;/label&gt;
    &lt;label&gt;&lt;input type="radio" name="question1" value="false" /&gt; False &lt;/label&gt;
&lt;/p&gt;
FIRST;

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

$selected_radio = $_POST['question1'];

if ($selected_radio == 'false') {
   $correct++;
print $correct;
   $_SESSION["correct"] = $correct;

}
}
}


if ($number == 1){
print &lt;&lt;&lt;SECOND
&lt;p&gt;2. In what state was the battle of Gettysburg fought?&lt;/p&gt;
&lt;p&gt;
    &lt;label&gt;&lt;input type="checkbox" name="question2" value="Texas" /&gt; a) Texas  
&lt;/label&gt;&lt;br/&gt;
    &lt;label&gt;&lt;input type="checkbox" name="question2" value="Pennsylvania" /&gt; b)  
Pennsylvania &lt;/label&gt;&lt;br/&gt;
    &lt;label&gt;&lt;input type="checkbox" name="question2" value="Virginia" /&gt; c) Virginia   
&lt;/label&gt;&lt;br/&gt;
    &lt;label&gt;&lt;input type="checkbox" name="question2" value="West Virginia" /&gt; d) West   
Virginia &lt;/label&gt;
&lt;/p&gt;
SECOND;

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

if(isset($_POST['question2']) &amp;&amp;
$_POST['question'] == 'Pennsylvania')
{
   $correct++;
   $_SESSION["correct"] = $correct;  
}

}
}


if ($number == 2){
print &lt;&lt;&lt;THIRD
&lt;p&gt;3. The last name of the commander of the Army of North Virginia was __________.&lt;/p&gt;
&lt;p&gt;
    &lt;input type='text' id='question3' /&gt;
THIRD;

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

$selected_answer = $_POST['question3'];

if ($selected_answer == "lee") {
   $correct++;
   $_SESSION["correct"] = $correct;
 }
 }
 }


if ($number &gt;= $total_number)
{
print &lt;&lt;&lt;FINAL_SCORE
Your final score is $correct correct out of $total_number. &lt;br /&gt;&lt;br /&gt;
Thank you for playing. &lt;br /&gt;&lt;br /&gt;
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print &lt;&lt;&lt;FORM
&lt;form method = "post" action = $script&gt;
&lt;input type = "submit" value = "Check Answer" /&gt;
&lt;/form&gt;
FORM;
}

?&gt;
</code></pre>

<p>Julio, my current code (2nd Update):</p>

<pre><code>&lt;?php
ini_set('session.gc_maxlifetime',900);
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

if(isset($_SESSION["correct"])){
$correct = $_SESSION["correct"];
} else {
$number = 0;
$correct = 0;
}

// check if which question was submitted
if (isset($_POST['submit'])) {
// set $number = the question that was submitted
$number = $_POST['question'];
switch ($_POST['question']){
    case 1:
        if ($_POST['answer']) $correct++; // this answer should be 'true'
        break;
    case 2:
        if ($_POST['answer'] == 2) $correct++; // this answer should be 'PA'
        break;
    case 3:
    if ($_POST['answer'] == "lee") $correct++; //this answer should be 'lee'
}
}   

// set the session correct var to our current tally
$_SESSION['correct'] = $correct;



$total_number = 3;

print &lt;&lt;&lt;TOP
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; History Quiz &lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt; History Quiz &lt;/h3&gt;
TOP;

if ($number == 0){
print &lt;&lt;&lt;FIRST
&lt;p&gt; You will be given $total_number questions in this quiz. &lt;br /&gt;&lt;br/&gt;
  You will have 15 minutes to complete it. &lt;br /&gt;&lt;br/&gt;
  You cannot go back to change previous answers.&lt;br /&gt;&lt;br/&gt;
  Here is your first question: &lt;br /&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;1. Abe Lincoln was born in Illinois.&lt;/p&gt;
&lt;p&gt;
    &lt;label&gt;&lt;input type="radio" name="answer" value="true" /&gt; True &lt;/label&gt;
    &lt;label&gt;&lt;input type="radio" name="answer" value="false" /&gt; False &lt;/label&gt;
    &lt;input type="hidden" name="question" value="1" /&gt;
&lt;/p&gt;
FIRST;
}


if ($number == 1){
print &lt;&lt;&lt;SECOND
&lt;p&gt;2. In what state was the battle of Gettysburg fought?&lt;/p&gt;
&lt;p&gt;
&lt;label&gt;&lt;input type="checkbox" name="answer" value="1" /&gt; a) Texas  
&lt;/label&gt;&lt;br/&gt;
&lt;label&gt;&lt;input type="checkbox" name="answer" value="2" /&gt; b)  
Pennsylvania &lt;/label&gt;&lt;br/&gt;
&lt;label&gt;&lt;input type="checkbox" name="answer" value="3" /&gt; c) Virginia   
&lt;/label&gt;&lt;br/&gt;
&lt;label&gt;&lt;input type="checkbox" name="answer" value="4" /&gt; d) West   
Virginia &lt;/label&gt;
&lt;input type="hidden" name="question" value="2" /&gt;
&lt;/p&gt;
SECOND;
}
if ($number == 2){
print &lt;&lt;&lt;THIRD
 &lt;p&gt;3. The last name of the commander of the Army of North Virginia was __________.&lt;/p&gt;
 &lt;p&gt;
    &lt;input type='text' id='answer' /&gt;
&lt;input type="hidden" name="question" value="3" /&gt;
 &lt;/p&gt;
THIRD;
}

if ($number &gt;= $total_number)
{
print &lt;&lt;&lt;FINAL_SCORE
Your final score is $correct correct out of $total_number. &lt;br /&gt;&lt;br /&gt;
Thank you for playing. &lt;br /&gt;&lt;br /&gt;
FINAL_SCORE;
session_destroy();
}
else {
 $number++;
 $_SESSION['number'] = $number;
 $script = $_SERVER['PHP_SELF'];
 print &lt;&lt;&lt;FORM
&lt;form method = "post" action = $script&gt;
&lt;input type = "submit" value = "Check Answer" /&gt;
&lt;/form&gt;
FORM;
}


?&gt;
</code></pre>
