How to take this notice Notice: Undefined index:

0

I have a $posicoes variable that receives the amount of elements that exist in the replies session:

$posicoes = count($_SESSION['respostas']);

if ($_GET['idQuestao'] > $posicoes) {
        // acontece isso... 
} else {
     $voltaQuestao = $idQuestao + 1;
     header("Location: questao.php?idQuestao=" . $voltaQuestao);
}

Dai keeps appearing this message every time the variable positions is 0

  

Notice: Undefined index: answers in C: \ wamp \ www \ quiz \ questao.php on line 23

Line 23 is this: GET

And I need to do this check, it has to start with 0 anyway. Does the error message appear when the variable positions is greater than or equal to 1 ?

    
asked by anonymous 23.09.2016 / 02:22

1 answer

1

On line 23 :

$posicoes = isset($_SESSION['respostas']) ? count($_SESSION['respostas']) : 0;

The isset (isset($_SESSION['respostas']) , verifies that the variable has been initialized, no longer giving the described error as index is undefined , that is, it does not exist!

Reference:

23.09.2016 / 02:35