php 我应该在哪里使用 isset() 和 !empty()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1219542/
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
In where shall I use isset() and !empty()
提问by shin
I read somewhere that the isset()function treats an empty string as TRUE, therefore isset()is not an effective way to validate text inputs and text boxes from a HTML form.
我在某处读到该isset()函数将空字符串视为TRUE,因此isset()不是验证 HTML 表单中的文本输入和文本框的有效方法。
So you can use empty()to check that a user typed something.
所以你可以empty()用来检查用户是否输入了一些东西。
Is it true that the
isset()function treats an empty string asTRUE?Then in which situations should I use
isset()? Should I always use!empty()to check if there is something?
该
isset()函数是否将空字符串视为TRUE?那么我应该在哪些情况下使用
isset()?我应该总是!empty()用来检查是否有东西吗?
For example instead of
例如代替
if(isset($_GET['gender']))...
Using this
使用这个
if(!empty($_GET['gender']))...
回答by dassouki
FTA:
自贸协定:
"isset() checks if a variable has a value including (False, 0 or empty string), but not NULL. Returns TRUE if var exists; FALSE otherwise.
On the other hand the empty() function checks if the variable has an empty value empty string, 0, NULL or False. Returns FALSE if var has a non-empty and non-zero value."
"isset() 检查变量是否具有包含(False、0 或空字符串)但不包含 NULL 的值。如果 var 存在,则返回 TRUE;否则返回 FALSE。
另一方面,empty() 函数检查变量是否具有空值空字符串,0、NULL 或 False。如果 var 具有非空和非零值,则返回 FALSE。”
回答by Pascal MARTIN
In the most general way :
以最一般的方式:
issettests if a variable (or an element of an array, or a property of an object) exists(and is not null)emptytests if a variable (...) contains some non-empty data.
To answer question 1:
回答问题1:
$str = '';
var_dump(isset($str));
gives
给
boolean true
Because the variable $strexists.
因为变量$str存在。
And question 2:
和问题2:
You should use isset to determine whether a variable exists; for instance, if you are getting some data as an array, you might need to check if a key isset in that array.
Think about $_GET/ $_POST, for instance.
您应该使用 isset 来确定变量是否存在;例如,如果您以数组的形式获取一些数据,您可能需要检查该数组中是否设置了键。
例如,
想想$_GET/ $_POST。
Now, to work on its value, when you know there is such a value : that is the job of empty.
现在,要研究其价值,当您知道有这样一个价值时:这就是empty.
回答by Pascal MARTIN
Neither is a good way to check for valid input.
两者都不是检查有效输入的好方法。
isset()is not sufficient because – as has been noted already –?it considers an empty string to be a valid value.! empty()is not sufficient either because it rejects '0', which could be a valid value.
isset()是不够的,因为——正如已经指出的那样——它认为一个空字符串是一个有效值。! empty()还不够,因为它拒绝可能是有效值的“0”。
Using isset()combined with an equality check against an empty string is the bare minimum that you need to verify that an incoming parameter has a value without creating false negatives:
使用isset()与对空字符串相等性检查相结合是最起码的,你需要验证传入的参数有没有产生假阴性值:
if( isset($_GET['gender']) and ($_GET['gender'] != '') )
{
...
}
But by "bare minimum", I mean exactly that. All the above code does is determine whether there is some value for $_GET['gender']. It does notdetermine whether the value for $_GET['gender']is valid(e.g., one of ("Male", "Female","FileNotFound")).
但我所说的“最低限度”,正是这个意思。以上代码所做的就是确定$_GET['gender']. 它并没有确定的值是否$_GET['gender']是有效的(例如,一个)。("Male", "Female","FileNotFound")
For that, see Josh Davis's answer.
为此,请参阅Josh Davis 的回答。
回答by Gumbo
issetis intended to be used only for variables and not just values, so isset("foobar")will raise an error. As of PHP 5.5, emptysupports both variables and expressions.
isset旨在仅用于变量而不仅仅是值,因此isset("foobar")会引发错误。从 PHP 5.5 开始,empty支持变量和表达式。
So your first question should rather be if issetreturns truefor a variable that holds an empty string. And the answer is:
因此,您的第一个问题应该是对于包含空字符串的变量是否isset返回true。答案是:
$var = "";
var_dump(isset($var));
The type comparison tablesin PHP's manual is quite handy for such questions.
PHP 手册中的类型比较表对于此类问题非常方便。
issetbasically checks if a variable has any value other than nullsince non-existing variables have always the value null. emptyis kind of the counter part to issetbut does also treat the integer value 0and the string value "0"as empty. (Again, take a look at the type comparison tables.)
isset基本上检查变量是否具有除null以外的任何值,因为不存在的变量始终具有值null。empty是一种计数器部分,isset但也将整数值0和字符串值"0"视为空。(再次查看类型比较表。)
回答by macio.Jun
If you have a $_POST['param'] and assume it's string type then
如果你有一个 $_POST['param'] 并假设它是字符串类型,那么
isset($_POST['param']) && $_POST['param'] != '' && $_POST['param'] != '0'
is identical to
等同于
!empty($_POST['param'])
回答by Josh Davis
isset() is not an effective way to validate text inputs and text boxes from a HTML form
isset() 不是验证 HTML 表单中的文本输入和文本框的有效方法
You can rewrite that as "isset() is not a way to validate input."To validate input, use PHP's filter extension. filter_has_var()will tell you whether the variable exists while filter_input()will actually filter and/or sanitize the input.
您可以将其重写为“isset() 不是一种验证输入的方法”。要验证输入,请使用 PHP 的过滤器扩展。filter_has_var()会告诉您变量是否存在,而filter_input()实际上会过滤和/或清理输入。
Note that you don't have to use filter_has_var()prior to filter_input()and if you ask for a variable that is not set, filter_input()will simply return null.
请注意,您不必使用filter_has_var()before filter_input(),如果您要求未设置的变量,则filter_input()只需 return null。
回答by Fokwa Best
When and how to use:
何时以及如何使用:
- isset()
- isset()
Truefor 0, 1, empty string, a string containing a value, true, false
真为0,1,空字符串,其中包含一个值的字符串,真,假
Falsefor null
为空为假
e.g
例如
$status = 0
if (isset($status)) // True
$status = null
if (isset($status)) // False
- Empty
- 空的
Falsefor 1, a string containing a value, true
假为 1,一个包含值的字符串,真
Truefor null, empty string, 0, false e.g
真为空,空字符串,0,假例如
$status = 0
if(empty($status)) // true
$status = 1
if(empty($status)) // False
回答by Mike
isset is used to determine if an instance of something exists that is, if a variable has been instantiated... it is not concerned with the value of the parameter...
isset 用于确定某个事物的实例是否存在,也就是说,如果变量已被实例化……它与参数的值无关……
Pascal MARTIN... +1 ...
帕斯卡·马丁... +1 ...
empty() does not generate a warning if the variable does not exist... therefore, isset() is preferred when testing for the existence of a variable when you intend to modify it...
如果变量不存在,empty() 不会生成警告...因此,当您打算修改变量时,在测试变量是否存在时,最好使用 isset()...
回答by moo
isset($variable) === (@$variable !== null)
empty($variable) === (@$variable == false)
回答by You
Using emptyis enough:
使用empty就足够了:
if(!empty($variable)){
// Do stuff
}
Additionally, if you want an integer value it might also be worth checking that intval($variable) !== FALSE.
此外,如果您想要一个整数值,也可能值得检查intval($variable) !== FALSE.

