I am setti开发者_如何学Gong a session variable inside a function studNameDetails1()
while I am trying to retrieve it in a function ViewMark()
. These are my two functions but there is no result:
function studNameDetails1()
{
$_SESSION['ATTsub']=$sub_id = $ID[5];
}
function ViewMark()
{
echo $_SESSION['ATTsub'];
}
When I echo the value in viewMark()
, there is no value.
The $ID
variable has not been declared inside the studNameDetails1 function, so when you try to access it, an undefined value. This will set the $_SESSION['ATTsub']
and $sub_id
variables to undefined, and so they will show as being empty when you try to print them.
If these variables have been declared outside the scope of the function, use the global keyword:
function studNameDetails1()
{
global $sub_id, $ID;
$_SESSION['ATTsub']=$sub_id = $ID[5];
}
http://php.net/manual/en/language.variables.scope.php
This is the way to handle sessions.
<?php // this starts the session
session_start();
// echo variable from the session, we set this on our other page
echo "Our color value is ".$_SESSION['color'];
echo "Our size value is ".$_SESSION['size'];
echo "Our shape value is ".$_SESSION['shape']; ?>
All of the values are stored in the $_SESSION array, which we access here. Another way to show this is to simply run this code:
<?php session_start(); Print_r ($_SESSION); ?>
精彩评论