获取变量超出大括号

bɪˈ*_*ɪnə 3 php variables

在这段代码中,我已经定义了变量id,如果我将使用$id外部花括号,我将获得错误未定义变量$id.有什么方法可以使用括号外的变量.

<?php
if (isset($_GET['username'])) {
$check = $db->prepare("SELECT id, email, phone, FROM members WHERE username=:username");
$check->execute(array(':username'=>$username));    
$get = $check->fetch(PDO::FETCH_ASSOC);
$id = $get['id'];
$email = $get['email'];
}
 else {
 // error
}

$posts =$db->prepare("SELECT * FROM posts WHERE id=:id");
$posts->execute(array(':id'=>$id));
$row=$posts->fetch(PDO::FETCH_ASSOC));
$title = $row['title'];
$body = $row['body']; 
?>
Run Code Online (Sandbox Code Playgroud)

Man*_*wad 5

<?php
 $id=""; //initialize a variable with blank value
 if (isset($_GET['username'])) {
 // query to get data
 $id = $_GET['id'];
 }
 else {
 // error
 }
?>
Run Code Online (Sandbox Code Playgroud)