简体   繁体   English

如何在PHP中检查变量是否为null

[英]How to check whether variable is null or not in PHP

In PHP, 在PHP中,

I am using One Variable called $token. 我正在使用一个称为$ token的变量。

I want to check whether it's value is null or not.I have tried by checking empty/isset/is_null in if condition.But It didn't work. 我想检查它的值是否为null。我尝试通过检查if条件中的empty/isset/is_null来尝试,但是没有用。

when I print its value using error_log($token) then it gives output like : " (null) ". 当我使用error_log($token)打印其值时,它将给出类似“ (null) ”的输出。

Because in database its value is " (null) ". 因为在数据库中它的值是“ (null) ”。

How can I check in php whether it is (null) or not. 如何检查php是否为(null)

Please guide me on this. 请指导我。

It looks like you're working not with a real null , but with a string - that's literally '(null)' . 看起来您不是在使用真正的null ,而是在使用字符串-实际上是'(null)' So check for that instead, with... 因此,请改用...

if ($token === '(null)') { ... }

What's more important, however, is why this value appeared in the DB in first place. 但是,更重要的是为什么此值首先出现在数据库中。 Unless there's a very specific (and valid) reason to do this, you'd better store nulls (absence of information) as plain NULL value, to avoid confusion such in this case both on application and database layer. 除非有非常具体(有效)的理由,否则最好将空值(缺少信息)存储为纯NULL值,以避免在应用程序层和数据库层出现这种情况下的混淆。

What you did is also right. 您所做的也是正确的。 Even You can have some more options try following conditions 即使您可以有更多选择,请尝试以下条件

1. if($token === null){....}
2. if($token == null){....}
3. if($token == ""){....}
  1. init some a string variable 初始化一些字符串变量

    $tmp_string = ""; $ tmp_string =“”; $tmp_string .= $token if($tmp_string== ""){....} $ tmp_string。= $ token if($ tmp_string ==“”){....}

Try All above. 尝试所有以上。 All The Best.... 祝一切顺利....

或最短的方法

if($token) echo 'not null';

我使用strlen() condition解决了我的问题。谢谢大家的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM