我试过了
$cookie = $_COOKIE['cookie'];如果cookie未设置,则会显示错误
PHP ERROR
Undefined index: cookie我如何防止它给我一个空的variable>
发布于 2011-05-03 00:00:03
使用isset查看cookie是否存在。
if(isset($_COOKIE['cookie'])){
$cookie = $_COOKIE['cookie'];
}
else{
// Cookie is not set
}发布于 2011-05-02 23:59:20
您可以使用array_key_exists实现此目的,如下所示:
$cookie = array_key_exists('cookie', $_COOKIE) ? $_COOKIE['cookie'] : null;发布于 2011-05-02 23:59:06
这取决于你的需要。
// If not set, $cookie = NULL;
if (isset($_COOKIE['cookie'])) { $cookie = $_COOKIE['cookie']; }或
// If not set, $cookie = '';
$cookie = (isset($_COOKIE['cookie'])) ? $_COOKIE['cookie'] : '';或
// If not set, $cookie = false;
$cookie = (isset($_COOKIE['cookie'])) ? $_COOKIE['cookie'] : false;参考文献:
https://stackoverflow.com/questions/5859333
复制相似问题