php: '0' 作为带有空的字符串()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4139301/
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
php: '0' as a string with empty()
提问by laukok
I want a 0 to be considered as an integer and a '0' to be considered as a string but empty() considers the '0' as a string in the example below,
我希望将 0 视为整数,将 '0' 视为字符串,但在下面的示例中,empty() 将 '0' 视为字符串,
$var = '0';
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is empty';
}
how can I 'make' empty() to take '0's as strings?
我怎样才能“使”为空()以将“0”作为字符串?
thanks.
谢谢。
回答by Jim W.
You cannot make it. That is how it was designed. Instead you can write an and
statement to test:
你做不到。这就是它的设计方式。相反,您可以编写一个and
语句来测试:
if (empty($var) && $var !== '0') {
echo $var . ' is empty';
}
You could use isset
, unless of course, you want it to turn away the other empties, that empty
checks for.
您可以使用isset
,当然,除非您希望它关闭empty
检查的其他空容器。
Edit
编辑
Fixed the check to do a type check as well, thanks to the 2371 for pointing that out :)
修复了检查以进行类型检查,感谢 2371 指出这一点:)
回答by Gordon
You cannot with empty
. From the PHP Manual:
你不能用empty
. 来自 PHP 手册:
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
以下内容被认为是空的:
- ""(空字符串)
- 0(0 作为整数)
- “0”(0 作为字符串)
- 空值
- 错误的
- array()(空数组)
- var $var; (声明的变量,但在类中没有值)
You have to add an additional other check.
您必须添加额外的其他检查。
回答by netcoder
You can't with only empty(). See the manual. You can do this though:
你不能只用空()。请参阅手册。你可以这样做:
if ($var !== '0' && empty($var)) {
echo "$var is empty and is not string '0'";
}
Basically, empty() does the same as:
基本上,empty() 与以下内容相同:
if (!$var) ...
But doesn't trigger a PHP Notice when the variable is not set.
但在未设置变量时不会触发 PHP 通知。
回答by Hamish
You can't. From the manual
你不能。从手册
Returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
如果 var 具有非空和非零值,则返回 FALSE。
以下内容被认为是空的:
- ""(空字符串)
- 0(0 作为整数)
- “0”(0 作为字符串)
- 空值
- 错误的
- array()(空数组)
- var $var; (声明的变量,但在类中没有值)
回答by manish1706
PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.
PHP 有不同的函数可以用来测试变量的值。三个有用的函数是 isset()、empty() 和 is_null()。所有这些函数都返回一个布尔值。如果这些功能没有以正确的方式使用,它们可能会导致意外的结果。
isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.
isset() 和 empty() 通常被视为相反的函数,但这并不总是正确的。在这篇文章中,我将解释这些功能之间的区别。
isset()
isset()
isset — Determine if a variable is set and is not NULL
In other words, it returns true only when the variable is not null. empty()
换句话说,只有当变量不为空时它才返回真。空的()
empty — Determine whether a variable is empty
In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable. is_null()
换句话说,如果变量是空字符串、false、array()、NULL、“0?、0 和未设置的变量,它将返回 true。一片空白()
is_null — Finds whether a variable is 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() 只能应用于声明的变量。
The table in picture is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
图中的表格是这些函数针对不同值返回的内容的简单参考。空格表示函数返回 bool(false)。
Also i have made custom function for checking all stuff.
我还制作了用于检查所有内容的自定义函数。
function checkEmpty($var, $term = ""){
if(isset($var) && trim($var) != "" && (!empty($var) || trim($var) == 0)){
return true;
}
else{
if($term != ""){
return array("status" => "error", "desc" => "$term can not be empty");
}
else{
return array("status" => "error", "desc" => "value can not be empty");
}
}
}
回答by Stemar
I always add to my codebase
我总是添加到我的代码库
function is_blank($value) {
return empty($value) && !is_numeric($value);
}
and use it instead of empty(). It solves the issue of keeping zeros (int, float or string) as non-empty.
并使用它代替empty()。它解决了将零(int、float 或 string)保持为非空的问题。
See http://www.php.net/manual/en/function.empty.php#103756which was added May 2011.
请参阅2011 年 5 月添加的http://www.php.net/manual/en/function.empty.php#103756。
回答by Jason McCreary
In both of your cases empty()
will return true
. Check the doc - http://php.net/empty.
在你的两种情况下empty()
都会返回true
。检查文档 - http://php.net/empty。
I suggest using a different function to match your spec.
我建议使用不同的功能来匹配您的规格。
回答by 2371
$var = '0';
// Evaluates to true because $var is empty
if (empty($var) && $var !== '0') {
echo '$var is empty or the string "0"';
}
回答by user187291
empty
is by far the most confusing and useless function in the php reperttheitroade. Don't use it.
empty
是迄今为止 php 库中最令人困惑和无用的函数。不要使用它。
There are three separate things you want to know when checking a value.
检查值时,您需要了解三件事。
- the value exists (use isset)
- the value has a specific type (use is_xxx)
- the value has specific properties (use comparison operators,
strpos
or regular expressions).
- 该值存在(使用isset)
- 该值具有特定类型(使用 is_xxx)
- 该值具有特定属性(使用比较运算符
strpos
或正则表达式)。
(the last two can be combined into one with typecasts or '===').
(最后两个可以通过类型转换或“===”合并为一个)。
Examples:
例子:
if(isset($var) && is_string($var) && strlen($var) > 0)...
if(isset($var) && intval($var) > 0)...
if(isset($var) && $var === '0')...
This seems more verbose, but shows clearly what you're doing. For structural objects it often makes sense to have a shortcut getter, e.g.
这看起来更冗长,但清楚地显示了你在做什么。对于结构对象来说,有一个捷径 getter 通常是有意义的,例如
/// get a string
function s($ary, $key, $default = '') {
if(!isset($ary[$key])) return $default;
$s = trim($ary[$key]);
return strlen($s) ? $s : $default;
}
/// get a natural number
function n($ary, $key, $default = 0) {
$n = intval(s($ary, $key));
return $n > 0 ? $n : $default;
}
$name = s($_POST, 'name');
$age = n($_POST, 'age');
回答by Viper_Sb
In this case don't use empty, use isset() in place of it. This will also allow 0 as an integer.
在这种情况下,不要使用空的,使用 isset() 代替它。这也将允许 0 作为整数。
$var = '0';
if (!isset($var)) {
print '$var is not set';
}
$var = 0;
if (!isset($var)) {
print '$var is not set';
}
Neither should print anything.
也不应该打印任何东西。