-1

I've been fiddling with PHP recently along with databases and I've come across a problem where I when I want to increment a session variable, question_id within an if statement it will increment once and not increment thereafter.

session_start();
$_SESSION['question_id'] = 11; // Set default var

if(isset($_POST['ans'])) {
    if($funcs->checkAnswer($_SESSION['question_id'], $mysqli)) {
        $_SESSION['question_id'] = $_SESSION['question_id'] + 1;
    }
}

$question = $funcs->getQuestion($_SESSION['question_id'], $mysqli);

The $_POST variable ans is the ID of a form where the user puts their answer, this answer is checked against the correct answer in the database by checkAnswer() which grabs the correct answer and compares it to the users' answer, if correct it returns true, else false. If checkAnswer() is true, in theory the session variable question_id should increment each time and thus update the question, however it does this only once then after this it stays on the first iteration of the increment (question 2), and if the answer inputted by the user is wrong, it resets back to question one for some reason.

Does anyone know why the $_SESSION['question_id'] only increments once even though the if statement is true? If you can see where the reset problem lies also that'd be great but I'll probably create a new post for that.

Thanks in advance.

2
  • 10
    Because the first thing you seem to do is reset it to 11 on every request... Commented Jun 29, 2016 at 15:09
  • 2
    I hate my life, thanks Jon :)) Commented Jun 29, 2016 at 15:10

3 Answers 3

2

You always reset your question_id to 11. You have to write:

if (empty($_SESSION['question_id']) {
    $_SESSION['question_id'] = 11;
}   
Sign up to request clarification or add additional context in comments.

Comments

0

You are executing $_SESSION['question_id'] = 11; on every request to "set default var" and then increment it once if the two conditions pass. Why are you expecting it to ever be higher than 12?

If you don't want to overwrite the value each time, you'll have to check it with isset:

if (!isset($_SESSION['question_id'])) {
    $_SESSION['question_id'] = 11;
}

Also you can do:

++$_SESSION['question_id'];

Instead of:

$_SESSION['question_id'] = $_SESSION['question_id'] + 1;

2 Comments

I'm just curious what is the difference to perform ++$var instead of $var++ in that particular case when this variable is not involved anywhere else?
++$var increments the variable and then returns the new value, $var++ stores the current value, increments the variable and returns the previously stored value. If you don't need the behavior of $var++ it's a tiny bit faster to use ++$var.
0

You are re-setting the value of $_SESSION['question_id'] each time the script gets executed. This is why it will only increment once. You should check it first like this:

<?php
session_start();

if( !isset($_SESSION['question_id']) )
{
  $_SESSION['question_id'] = 11; // Set default var
}

if(isset($_POST['ans'])) {
    if($funcs->checkAnswer($_SESSION['question_id'], $mysqli)) {
        $_SESSION['question_id'] = $_SESSION['question_id'] + 1;
    }
}

$question = $funcs->getQuestion($_SESSION['question_id'], $mysqli);

This way you prevent the session var being set repeatedly.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.