在if语句之外获取变量

Hi guys I have a problem with getting a variable outside and after an if statement. Little code explanation: $gameid or $id is sent via ajax to this file. Now $id have always a value and if (isset($_GET['id'])) { is true. The insert and select querys work very well. The problem is that I cant get the variable $answer or $question[0]['question']; after the second if statement. I can get them when I change the second if (isset($_GET['id'])) { to else { but then anyhow there become two rows in my database inserted, the first is right and the second one is empty. Now why cant I get the variables $answer and $question[0]['question']; after the second if condition?

The error log shows: Notice undefined variable question.

<?php

$hostname='localhost';
$user='';
$password='';
if (isset($_GET['gameid'])) {
    $gameid = $_GET['gameid'];
}
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    echo $id;
}
if (isset($_GET['questionid'])) {
    $questionid = $_GET['questionid'];
}
$new = 0;

if (isset($_GET['gameid'])) {

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
        $sql = "SELECT *
                FROM questions_de 
                WHERE id = '$questionid'
                LIMIT 1";

        if ($res = $dbh->query($sql)) {// need to add this line in your code
            // then after fetchColumn
            $question = $res->fetchAll();
        }

        if($question > 0) {
            //do something
        } else {
            echo "Sorry something happen wrong with our servers.";
        }
    }
    catch(PDOException $e) {
    }

    if ($question[0]["answerm²"] == 0 && $question[0]["answerm³"] == 0) {
        $answer = "answer_m";
    } else {
        $answer = "answerm³";
    }
}
if (isset($_GET['id'])) {
    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
        $sql = "SELECT username 
                FROM user 
                WHERE id = '$id' 
                LIMIT 1";

        if ($res = $dbh->query($sql)) {// need to add this line in your code
            // then after fetchColumn
            $user2name = $res->fetchAll();
        }

        if($user2name > 0) {
            //do something
        } else {
            echo "Sorry something happen wrong with our servers.";
        }
    }
    catch(PDOException $e) {


    }

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
        $sql = "SELECT *
                FROM questions_de
                LIMIT 1"; // 
        if ($res = $dbh->query($sql)) {// need to add this line in your code
            // then after fetchColumn
            $question = $res->fetchAll();
        }

        if($question > 0) {
            //do something
        } else {

            echo "Sorry something happen wrong with our servers.";
        }
    }

    catch(PDOException $e) {

    }


    if ($question[0]["answerm²"] == 0 && $question[0]["answerm³"] == 0) {
        try {
            $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);

            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
            $sql = "INSERT INTO game_create (user1, user2, user1name, user2name, question, questionid, answer)
                    VALUES ('".$_COOKIE["userid"]."', '".$id."', '".$_COOKIE["username"]."', '".$user2name[0]["username"]."', '".$question[0]['question']."', '".$question[0]['id']."', '".$question[0]['answer_m']."')";

            if ($dbh->query($sql)) {
                //echo "New Record Inserted Successfully";
            } else{
                // echo "Data not successfully Inserted.";
            }

            $new = $dbh->lastInsertId();
            $dbh = null;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

        if ($new > 0) {

        } else {
            echo 'Sorry something went wrong.';
        }
        $answer = "answer_m";
    }
    else  {
        try {
            $dbh = new PDO("mysql:host=$hostname;dbname=max_com_db_socame",$user,$password);

            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
            $sql = "INSERT INTO game_create (user1, user2, user1name, user2name, question, questionid, answer)
                    VALUES ('".$_COOKIE["userid"]."', '".$id."', '".$_COOKIE["username"]."', '".$user2name[0]["username"]."', '".$question[0]['question']."', '".$question[0]['id']."', '".$question[0]['answer_m³']."')";

            if ($dbh->query($sql)) {
                //echo "New Record Inserted Successfully";
            } else{
                // echo "Data not successfully Inserted.";
            }
            $new = $dbh->lastInsertId();
            $dbh = null;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

        if ($new > 0) {

        } else {
            echo 'Sorry something went wrong.';
        }
        $answer = "answerm³";

    }
}
?>

On the line:

if (isset($_GET['gameid'])) {..}

You're checking if the $_GET['gameid'] is set, with other words, if it has a value. As you stated, $_GET['gameid'] only has a value when $_GET_[id] doesn't have value. As you said, $_GET_['id] always has value, so $_GET['gameid'] wont be initialized.

Therefor, it won't get past this condition, and won't reach the line of $question = $res->fetchAll();, which initializes the variable.

The same thing stands for $answer, since it's in the same if statement and is also doing conditional checking on the $question variable.

To solve this problem, either initialize the $_GET['gameid'] variable by sending it to the PHP script without any conditions or remove the if (isset($_GET['gameid'])) {..} statement.