How do check if a PHP session is empty?

后端 未结 8 1129
無奈伤痛
無奈伤痛 2020-12-02 15:50

Is this bad practice?

if ($_SESSION[\'something\'] == \'\')
{
    echo \'the session is empty\';
}

Is there a way to check if its empty or

8条回答
  •  醉话见心
    2020-12-02 15:59

    Use isset, empty or array_key_exists (especially for array keys) before accessing a variable whose existence you are not sure of. So change the order in your second example:

    if (!isset($_SESSION['something']) || $_SESSION['something'] == '')
    

提交回复
热议问题