检查$ _COOKIE值是否为空 [英] Checking if a $_COOKIE value is empty or not

查看:274
本文介绍了检查$ _COOKIE值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将cookie分配给变量:

I assign a cookie to a variable:

$user_cookie = $_COOKIE["user"];

如何检查$user_cookie是否收到一些值?

How can I check if the $user_cookie received some value or not?

我应该使用if (empty($user_cookie))还是其他名称?

Should I use if (empty($user_cookie)) or something else?

推荐答案

使用 isset ()像这样:

if (isset($_COOKIE["user"])){
$user_cookie = $_COOKIE["user"];
}

这告诉您$_COOKIE中是否存在名为user的密钥.该值本身可以是""0NULL等.根据上下文,其中某些值(例如0)可能是有效的.

This tells you whether a key named user is present in $_COOKIE. The value itself could be "", 0, NULL etc. Depending on the context, some of these values (e.g. 0) could be valid.

PS:对于第二部分,我将使用===运算符检查falseNULL0"",或者可能是(string) $user_cookie !== "".

PS: For the second part, I'd use === operator to check for false, NULL, 0, "", or may be (string) $user_cookie !== "".

这篇关于检查$ _COOKIE值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆