我正在尝试编写一个脚本,当用户上传文件但没有输入名称时,会返回错误.我尝试过使用is_null,empty和isset,它们都不起作用.例如,在下面,即使输入了名称,is_null也会返回错误.有人可以帮忙吗?
$caption = $_REQUEST[$name_input_name];
if(is_null($caption)) {
$file->error = 'Please Enter a Title';
return false;
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ins 11
isset()
将检查变量是否已设置,即
<?php
echo isset($var); // false
$var = 'hello';
Run Code Online (Sandbox Code Playgroud)
empty()
将检查变量是否为空,即
<?php
$emptyString = '';
echo empty($emptyString); // true
Run Code Online (Sandbox Code Playgroud)
is_null()
将检查NULL
哪个与空不同,因为它设置为NULL
不是空字符串.(NULL可能是一个令人困惑的概念)
由于你的标题是一个字符串,我想你想要使用 empty()
if (!isset($_REQUEST[$name_input_name]) || empty($_REQUEST[$name_input_name])) {
$file->error = 'Please Enter a Title';
return false;
}
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
在empty()函数文档页面上,您可以阅读:
以下内容被认为是空的:
....
是$ var; (声明的变量,但没有值)
代码$var;
是定义变量可能会产生误导,但不会为其赋值,但这是错误的.变量$var
仍未定义,类型识别函数,is_null()
如果$var
作为参数传递则发出警告.
但它不适合未结算的类或对象属性.声明它们而不指定一些值会自动指定NULL.
一些低级描述:
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
Run Code Online (Sandbox Code Playgroud)
此外,他们将通过相同的功能编译 zend_compile_isset_or_empty
功能is_null()
是类型识别功能,如is_numeric
,is_recource
,is_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);
}
Run Code Online (Sandbox Code Playgroud)
PHP empty()
VS is_null()
VS isset()
:
“” | “富” | 空值 | 错误的 | 0 | 不明确的 | |
---|---|---|---|---|---|---|
empty() |
真的 | 错误的 | 真的 | 真的 | 真的 | 真的 |
is_null() |
错误的 | 错误的 | 真的 | 错误的 | 错误的 | 真 (错误) |
isset() |
真的 | 真的 | 错误的 | 真的 | 真的 | 错误的 |
如果要检查是否存在除或undefined之外的任何值,请使用(因为会对未定义的变量生成警告。)null
isset($var)
!is_null()
如果您想检查该值是非空白文本还是包括零在内的任何数字,则会变得更加棘手:
if (!empty($v) || (isset($v) && ($v === 0 || $v === '0'))) {
// $v is non-blank text, true, 0 or '0'
// $v is NOT an empty string, null, false or undefined
}
Run Code Online (Sandbox Code Playgroud)
小智 6
从PHP手册 - isset():
isset - 确定变量是否已设置且不为NULL
换句话说,仅当变量不为null时才返回true.
从PHP手册 - 空():
empty - 确定变量是否为空
换句话说,如果变量是空字符串,false,array(),NULL,"0?",0和未设置的变量,它将返回true.
从PHP手册 - is_null():
is_null - 查找变量是否为NULL
换句话说,仅当变量为null时才返回true.除了可以应用于未知变量的一个差异,但仅适用于声明的变量之外,它is_null()
是相反的.isset()
isset()
is_null()
我认为您打算isset
在将其分配给某物之前使用:
if(!isset($_REQUEST[$name_input_name]))
{
$file->error = 'Please Enter a Title';
return false;
}
$caption = $_REQUEST[$name_input_name];
Run Code Online (Sandbox Code Playgroud)