php isset vs 空 vs is_null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12375833/
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-25 03:26:24  来源:igfitidea点击:

isset vs empty vs is_null

php

提问by user1605871

I'm trying to write a script that when a user uploads a file and does not enter a name an error is returned. I've tried using is_null, empty, and isset and they all do not work. Eg, below, is_null returns an error even when a name is entered. Can anyone help?

我正在尝试编写一个脚本,当用户上传文件但未输入名称时,会返回错误。我试过使用 is_null、empty 和 isset,但它们都不起作用。例如,在下面,即使输入了名称,is_null 也会返回错误。任何人都可以帮忙吗?

$caption = $_REQUEST[$name_input_name];

if(is_null($caption)) {
    $file->error = 'Please Enter a Title';
    return false;
}

采纳答案by Steve Robbins

isset()will check if the variable is set, ie

isset()将检查变量是否已设置,即

<?php

echo isset($var); // false

$var = 'hello';

empty()will check if the variable is empty, ie

empty()将检查变量是否为空,即

<?php

$emptyString = '';

echo empty($emptyString); // true

is_null()will check for NULLwhich is different from empty, because it's set to NULLnot an empty string. (NULL might be a confusing concept)

is_null()将检查NULL哪个与空不同,因为它设置NULL为非空字符串。(NULL 可能是一个令人困惑的概念)

Since your title is a string, I think you want to be using empty()

由于您的标题是一个字符串,我认为您想使用 empty()

if (!isset($_REQUEST[$name_input_name]) || empty($_REQUEST[$name_input_name])) {
    $file->error = 'Please Enter a Title';
    return false;
}

回答by rjhdby

is_null()emits WARNING in case if variable is not set, but isset()and empty()don't.

is_null()在发出警告的情况下,如果变量没有设置,但isset()empty()没有。

$a - variable with not null value (e.g. TRUE)
$b - variable with null value. `$b = null;`
$c - not declared variable
$d - variable with value that cast to FALSE (e.g. empty string, FALSE or empty array)
$e - variable declared, but without any value assigned
$a->a - declared, but not assigned object property. (`public $a;`)
A::$a - declared, but not assigned static class property.

         |   $a  |   $b  |   $c  |   $d  |   $e  | $a->a | A::$a |
---------+-------+-------+-------+-------+-------+-------+-------+
is_null()| FALSE | TRUE  |TRUE*W | FALSE | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
isset()  | TRUE  | FALSE | FALSE | TRUE  | FALSE | FALSE | FALSE |
---------+-------+-------+-------+-------+-------+-------+-------+
empty()  | FALSE | TRUE  | TRUE  | TRUE  | TRUE  | TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
null === | FALSE | TRUE  |TRUE*W | FALSE | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
null ==  | FALSE | TRUE  |TRUE*W | TRUE  | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+

TRUE*W - function return TRUE, but same time emits WARNING.

On empty() function documentation pageyou can read, that:

empty() 函数文档页面上,您可以阅读:

The following things are considered to be empty:

....

$var; (a variable declared, but without a value)

以下内容被认为是空的:

....

$var; (声明的变量,但没有值)

It can be misleading that code $var;is define variable, but does not assign any value to it, but it is wrong. Variable $varis still undefined and type recognize functions, like is_null()emits warnings if you pass $varas an argument.

代码$var;是定义变量,但没有为其分配任何值可能会产生误导,但这是错误的。变量$var仍然未定义,类型识别函数,例如is_null()如果您$var作为参数传递,则会发出警告。

But it is not right for unsettled class or object properties. Declaring them without assign some value automatically assign NULL.

但它不适用于未解决的类或对象属性。声明它们而不分配一些值会自动分配 NULL。

UPDTyped properties in PHP 7.4 DO NOT assigned by NULL by default. If you does not set any value to them, they are considered as unassigned.

默认情况下,PHP 7.4 中的UPD类型属性不分配为 NULL。如果您没有为它们设置任何值,它们将被视为未分配。

Some low level descriptions:

一些低级描述:

isset()and empty()is core functions, that will be compiled directly to specific opcode according to zval type:

isset()empty()是核心函数,将根据 zval 类型直接编译为特定的操作码:

ZEND_ISSET_ISEMPTY_THIS
ZEND_ISSET_ISEMPTY_CV
ZEND_ISSET_ISEMPTY_VAR
ZEND_ISSET_ISEMPTY_DIM_OBJ
ZEND_ISSET_ISEMPTY_PROP_OBJ
ZEND_ISSET_ISEMPTY_STATIC_PROP

Furthermore they will compile by the same function zend_compile_isset_or_empty

此外,它们将通过相同的函数进行编译 zend_compile_isset_or_empty

Function is_null()is type recognizer function, like is_numeric, is_recource, is_bool, etc. And will be called like user-defined function with opcodes INIT_FCALL_BY_NAME/DO_FCALL_BY_NAMEand so.

函数is_null()类型识别器函数,如is_numericis_recourceis_bool等。 并且会像用户定义的函数一样调用操作码INIT_FCALL_BY_NAME/DO_FCALL_BY_NAME等。

/* {{{ proto bool is_null(mixed var)
   Returns true if variable is null
   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
PHP_FUNCTION(is_null)
{
    php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
}

回答by rjhdby

isset()

isset()

From PHP manual – isset():

来自 PHP 手册 –isset():

isset — Determine if a variable is set and is not NULL

isset — 确定变量是否已设置且不为 NULL

In other words, it returns true only when the variable is not null.

换句话说,只有当变量不为空时它才返回真。

empty()

空的()

From PHP Manual – empty():

来自 PHP 手册 – empty():

empty — Determine whether a variable is empty

empty - 确定变量是否为空

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

换句话说,如果变量是空字符串、false、array()、NULL、“0?、0 和未设置的变量,它将返回 true。

is_null()

一片空白()

From PHP Manual – is_null():

来自 PHP 手册 – is_null():

is_null — Finds whether a variable is NULL

is_null — 查找变量是否为 NULL

In other words, it returns true only when the variable is null. is_null()is opposite of isset(), except for one difference that isset()can be applied to unknown variables, but is_null()only to declared variables.

换句话说,只有当变量为空时它才返回真。is_null()与 相反isset(),除了一个区别isset()可以应用于未知变量,但is_null()只能应用于声明的变量。

回答by Ry-

I think you meant to use issetbeforeyou assigned it to something:

我认为您打算isset将其分配给某物之前使用:

if(!isset($_REQUEST[$name_input_name]))
{
    $file->error = 'Please Enter a Title';
    return false;
}

$caption = $_REQUEST[$name_input_name];

回答by Fifi

Not so easy to get the difference between isset(), is_null()and empty(). It is more subtle than than we would like to believe.

不是那么容易得到的区别isset()is_null()empty()。它比我们愿意相信的更微妙。

Since, it is not easy to clearly explain the difference with words, I suggest to look at this page: there is a detailed table containing behavior for each function. It clearly and fully explains the differences.

因为,用文字解释清楚区别并不容易,我建议看这个页面:有一个包含每个函数行为的详细表格。它清楚而充分地解释了差异。

By the way, I'm truly convinced than many PHP scripts contain security breach due to the misunderstood of the behaviors.

顺便说一句,我真的相信许多 PHP 脚本由于对行为的误解而包含安全漏洞。

回答by Antonio Petricca

Here isa very good explanation:

这里有一个很好的解释:

enter image description here

在此处输入图片说明

回答by Sumeet Kumar

You can try this :

你可以试试这个:

if(!isset($_REQUEST['name_input_name']))
{
    $file->error = 'Please Enter a Title';
    return false;
}
$caption = $_REQUEST['name_input_name'];

Note : $_REQUEST is an Global array that store the data in key=>value pair. consider "name_input_name" as value extracted from server.

注意:$_REQUEST 是一个全局数组,将数据存储在 key=>value 对中。将“name_input_name”视为从服务器提取的值。

if name_input_name is set to some value code will skip if block and store the value to variable $caption.

如果 name_input_name 设置为某个值,代码将跳过 if 块并将值存储到变量 $caption。

回答by gurghet

is_null is the dual of isset except isset does not print notices if the variable is null.

is_null 是 isset 的对偶,但如果变量为 null,则 isset 不会打印通知。

>$ciao;
>var_export(is_null($ciao));
>PHP Notice:  Undefined variable: ciao in php shell code on line 1
>true
>var_export(!isset($ciao));
>true

回答by Phil sims

I use strlen to count the number of characters

我使用 strlen 来计算字符数

 if (strlen($_REQUEST['name_input_name']) < 1) {
? ? $file->error = 'Please Enter a Title';
? ? return false;
}

回答by Kabir Hossain

  1. isset() — Determine if a variable is set and not NULL

  2. empty() - Determine if a variable is empty.

  3. is_null() - Determine if a variable is null

  1. isset() — 确定变量是否已设置且不为 NULL

  2. empty() - 确定变量是否为空。

  3. is_null() - 确定变量是否为空