php 检查变量是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8777591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:29:04  来源:igfitidea点击:

check if variable empty

phpif-statementnull

提问by ZeroSuf3r

if ($user_id == NULL || $user_name == NULL || $user_logged == NULL) {
    $user_id = '-1';
    $user_name = NULL;
    $user_logged = NULL;
}
if ($user_admin == NULL) {
    $user_admin = NULL;
}
  1. Is there any shortest way to do it ?
  2. And if i right, it should be tested with is_null?
  3. It's possible $user_id, $user_nameand $user_loggedwrite in one line (maybe array?) without repeating NULL?
  1. 有没有最短的方法来做到这一点?
  2. 如果我是对的,应该用is_null?
  3. 这是可能的$user_id$user_name并且$user_logged写在一行(也许是数组?)而不重复NULL

回答by Rob W

If you want to test whether a variable is reallyNULL, use the identity operator:

如果要测试变量是否为真的NULL,请使用恒等运算符:

$user_id === NULL  // FALSE == NULL is true, FALSE === NULL is false
is_null($user_id)

If you want to check whether a variable is not set:

如果要检查变量是否未设置:

!isset($user_id)

Or if the variable is not empty, an empty string, zero, ..:

或者,如果变量不为空,则为空字符串、零、..:

empty($user_id)

If you want to test whether a variable is not an empty string, !will also be sufficient:

如果要测试变量是否不是空字符串,!也足够了:

!$user_id

回答by Mark

You can check if it's not set (or empty) in a number of ways.

您可以通过多种方式检查它是否未设置(或为空)。

if (!$var){ }

Or:

或者:

if ($var === null){ } // This checks if the variable, by type, IS null.

Or:

或者:

if (empty($var)){ }

You can check if it's declared with:

您可以检查它是否声明为:

if (!isset($var)){ }

Take note that PHP interprets 0 (integer) and "" (empty string) and false as "empty" - and dispite being different types, these specific values are by PHP considered the same. It doesn't matter if $var is never set/declared or if it's declared as $var = 0 or $var = "". So often you compare by using the === operator which compares with respect to data type. If $var is 0 (integer), $var == "" or $var == false will validate, but $var === "" or $var === false will not.

请注意,PHP 将 0(整数)和“”(空字符串)和 false 解释为“空” - 尽管类型不同,但 PHP 认为这些特定值是相同的。$var 是否从未设置/声明或声明为 $var = 0 或 $var = "" 都没有关系。您经常使用 === 运算符进行比较,该运算符根据数据类型进行比较。如果 $var 为 0(整数),则 $var == "" 或 $var == false 将验证,但 $var === "" 或 $var === false 不会。

回答by user3470929

here i have explained how the empty function and isset works please use the one that is appropriate also you can use is_null function also

在这里我已经解释了空函数和 isset 是如何工作的,请使用合适的,你也可以使用 is_null 函数

<?php
    $val = 0;
    //evaluates to true because $var is empty
    if (empty($val)) {
        echo '$val is either 0, empty, or not set at all';
    }
    //evaluates to true because $VAR IS SET
    if (isset($val)) {
        echo '$val is set even though it is empty';
    }
    ?>

回答by Antony

Its worth noting - and I only found this out after nearly 9 years of PHP coding that the best way of checking any variable exists is using the empty() function. This is because it doesn't generate errors and even though you turn them off - PHP still generates them! empty() however won't return errors if the variable doesn't exist. So I believe the correct answer is not to check if its null but to do the following

值得注意的是 - 我在将近 9 年的 PHP 编码之后才发现这一点,检查任何变量是否存在的最佳方法是使用 empty() 函数。这是因为它不会生成错误,即使您关闭它们 - PHP 仍然会生成它们!但是如果变量不存在,empty() 不会返回错误。所以我相信正确的答案不是检查它是否为空,而是执行以下操作

if (!empty($var) && is_null($var))

Note the PHP manual

注意 PHP 手册

variable is considered empty if it does not exist or if its value equals FALSE

如果变量不存在或其值等于 FALSE,则变量被认为是空的

As opposed to being null which is handy here!

与 null 不同,这在这里很方便!

回答by Michael Berkowski

empty()is a little shorter, as an alternative to checking !$user_idas suggested elsewhere:

empty()有点短,作为检查!$user_id其他地方建议的替代方法:

if (empty($user_id) || empty($user_name) || empty($user_logged)) {
}

回答by Mike Q

Felt compelled to answer this because of the other responses. Use empty() if you can because it covers more bases. Future you will thank me.

由于其他回答,感到不得不回答这个问题。如果可以,请使用 empty() ,因为它涵盖了更多的碱基。未来你会感谢我的。

For example you will have to do things like isset() && strlen() where instead you could use empty(). Think of it like this empty is like !isset($var) || $var==false

例如,您将不得不执行isset() && strlen() 之类的操作,而您可以使用empty()。把它想象成这个空就像 !isset($var) || $var==false

回答by Mike Q

To check for null values you can use is_null()as is demonstrated below.

要检查空值,您可以使用is_null(),如下所示。

if (is_null($value)) {
   $value = "MY TEXT"; //define to suit
}

回答by Dmitry Zaytsev

1.

1.

if(!($user_id || $user_name || $user_logged)){
    //do your stuff
}

2 . No. I actually did not understand why you write such a construct.

2 . 不,我实际上不明白你为什么要写这样一个结构。

3 . Put all values into array, for example $ar["user_id"], etc.

3 . 将所有值放入数组,例如 $ar["user_id"] 等。

回答by Alnitak

Please define what you mean by "empty".

请定义您所说的“空”是什么意思。

The test I normally use is isset().

我通常使用的测试是isset().

回答by Vijeenrosh P.W

you can use isset() routine .

您可以使用 isset() 例程。

also additionaly you can refer an range of is_type () functions like

此外,您还可以参考一系列 is_type () 函数,例如

is_string(), is_float(),is_int() etc to further specificaly test

is_string(), is_float(),is_int() 等进一步具体测试