为什么变量在函数protect()输出什么? [重复]

This question already has an answer here:

I wrote this code to read the access leve of users logged in so, protect access to pages based on the access level of user logged in

and do function has sessionloginid and $v but $v variable output nothing what I need to edit in the function code? error message appear say :
Notice: Undefined variable: v

 if(isset($_SESSION['sessionloginid']))// point to id of user logged in
        {  
        $query =
        "SELECT * 
        FROM privilege 
        where login_id='".$_SESSION['sessionloginid']."'  " ;



    $access_level= array();
    $result = mysqli_query($link,$query) or die('');
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
            {


         $_SESSION['sessionloginid']= $row['login_id']; 
        $_SESSION['sessionaccess']= $row['AccessLevel'];//output 
         $access_level[]=$_SESSION['sessionaccess'];
        print_r ($access_level);//Array([0]=>1)Array([0]=>1[1]=>2) 

        }   }

foreach($access_level as $k => $v)
    {
        print_r($v) ;// output 12 

    }
function protect(){
 if($_SESSION['sessionloginid']==true && $v !=1)
 {
 echo $v; //output nothing
 }

}
    protect($v);
</div>

You need to pass in $v as a function parameter

protect($v);

At it's current scope, once the foreach loop ends, $v doesn't exist. It only exists within the loop, so you'll have to either call the function inside the loop, or set a variable on a higher scope to the value so you can use it elsewhere.