'0' PHP 中使用 empty() 作为字符串
我希望将 0 视为整数,将 '0' 视为字符串,但在下面的示例中,empty() 将 '0' 视为字符串,
$var = '0';
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is empty';
}
我如何“使”empty() 采取'0 作为字符串?
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,
$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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
你不能让empty()接受它。它就是这样设计的。相反,您可以编写一个
and
语句来测试:您可以使用
isset
,当然,除非您希望它拒绝其他empty
的空容器> 检查。You cannot make empty() take it. That is how it was designed. Instead you can write an
and
statement to test:You could use
isset
, unless of course, you want it to turn away the other empties thatempty
checks for.您不能使用
空
。 来自 PHP 手册:您必须添加额外的其他检查。
You cannot with
empty
. From the PHP Manual:You have to add an additional other check.
PHP 有不同的函数可用于测试变量的值。三个有用的函数是 isset()、empty() 和 is_null()。所有这些函数都返回一个布尔值。如果这些功能没有以正确的方式使用,可能会导致意想不到的结果。
isset() 和empty() 通常被视为相反的函数,但事实并非总是如此。在这篇文章中,我将解释这些函数之间的差异。
isset()
摘自 PHP 手册 – isset():
换句话说,仅当变量不为 null 时才返回 true。
empty()
摘自 PHP 手册 –empty():
,也就是说,如果变量是空字符串,则返回 true,如果为 false,则返回 array()、NULL、“0?”、0、未设置的变量。
is_null()
摘自 PHP 手册 – is_null():
换句话说,仅当变量为 null 时才返回 true。 is_null() 与 isset() 相反,除了 isset() 可以应用于未知变量,而 is_null() 只能应用于已声明的变量。
图中的表格是这些函数针对不同值将返回的内容的简单参考。空格表示函数返回 bool (false)。
此外,我还制作了一个自定义函数来检查所有内容:
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.
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()
From PHP manual – isset():
In other words, it returns true only when the variable is not null.
empty()
From PHP Manual – 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()
From PHP Manual – 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.
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).
Also I have made a custom function for checking all stuff:
你不能只用empty()。请参阅手册。不过,您可以这样做:
基本上,empty() 的作用与:
但当未设置变量时,它不会触发 PHP 通知。
You can't with only empty(). See the manual. You can do this though:
Basically, empty() does the same as:
But it doesn't trigger a PHP notice when the variable is not set.
你不能。来自手册
You can't. From the manual
empty
是 PHP 函数库中迄今为止最令人困惑和无用的函数。不要使用它。检查值时您需要了解三件不同的事情。
strpos
或正则表达式)。(最后两个可以通过类型转换或“===”组合成一个)。
示例:
这看起来更冗长,但它清楚地显示了您在做什么。对于结构对象来说,有一个快捷方式 getter 通常是有意义的,例如
empty
is by far the most confusing and useless function in the PHP repertoire. Don't use it.There are three separate things you want to know when checking a value.
strpos
or regular expressions).(the last two can be combined into one with typecasts or '===').
Examples:
This seems more verbose, but it shows clearly what you're doing. For structural objects it often makes sense to have a shortcut getter, e.g.
在这两种情况下,
empty()
都会返回true
。检查文档。我建议使用不同的函数来匹配您的规格。
In both of your cases
empty()
will returntrue
. Check the documentation.I suggest using a different function to match your specification.
我总是将其添加到我的代码库中:
并使用它而不是empty()。它解决了将零(整数、浮点数或字符串)保持为非空的问题。
请参阅 2011 年 5 月添加的 is_blank()。
I always add this to my codebase:
And use it instead of empty(). It solves the issue of keeping zeros (int, float or string) as non-empty.
See is_blank() which was added May 2011.
如果您想跳过空 $filter 并且不跳过 $filter = '0' 和其他值:
If you want skip an empty $filter and don’t skip $filter = '0' and other values:
在这种情况下,不要使用empty()。使用 isset() 代替它。这也将允许 0 作为整数。
两者都不应该打印任何东西。
In this case, don't use empty(). Use isset() in place of it. This will also allow 0 as an integer.
Neither should print anything.
不能,因为整数、字符串、浮点数和 null 对于 PHP 来说并不重要。
因为它很酷:)
您必须检查变量的特征: is_numeric (), isset(), === 、strlen()等例如
:
或
或其他示例:)
You can not, because integer, string, float, and null do not matter for PHP.
Because it is cool :)
You must check characteristic features for your variable: is_numeric(), isset(), ===, strlen(), etc.
For example:
or
or other examples :)