0

在传递$_SESSION['var']给函数时,如果$_SESSION['var']未设置,我会收到通知:

Notice: Undefined index: var in scripname.php on line 49

有没有更安全的方法来获取,如果未设置$_SESSION['var'],这会给我吗?0$_SESSION['var']

先感谢您!!

4

6 回答 6

5
$var = isset($_SESSION['var']) ? $_SESSION['var'] : 0
于 2012-05-08T07:41:48.813 回答
3
if (isset($_SESSION['var']))
{
    echo "session is set";
}
else
{
    echo "session is not set";
}
于 2012-05-08T08:01:05.553 回答
1
if (isset($_SESSION['var'])){
    // Do something
}
于 2012-05-08T07:41:40.723 回答
0
if(!array_key_exists('var',$_SESSION))
{
    $var = 0;
}
else
{
    $var = $_SESSION['var'];
}
于 2012-05-08T07:41:51.580 回答
0

采用

if(isset($_SESSION['var']))
    // do
于 2012-05-08T07:42:04.203 回答
0

怎么样:

$var = @$_SESSION['var'];
于 2012-05-08T07:51:28.367 回答