在函数外部使用查询值

i need to use a "question" variable outside the function:

public  function name(){
    if($_SESSION[agT] == "random" AND $_SESSION[ct]=="random"){
    $consult = DB::getConn()->prepare('SELECT * FROM table ORDER BY RAND() LIMIT 1');
    $consult->execute();
    $question=$consult->fetch(PDO::FETCH_ASSOC);
}

Like

echo "$question[column];
echo "$question[anothercolumn]; 

As others stated, you should learn the basics of procedural and functions programming. You can just return the value of the query from the function :

public  function_name(){
    if($_SESSION[agT] == "random" AND $_SESSION[ct]=="random"){
    $consult = DB::getConn()->prepare('SELECT * FROM table ORDER BY RAND() LIMIT 1');
    $consult->execute();
    $question=$consult->fetch(PDO::FETCH_ASSOC);

    return $question
}

$result = function_name();

echo $question['col1'];
echo $question['col2']; 

do return $question

then call this function

$val = name();