PHP-检查变量是否未定义
问题描述:
考虑此jquery语句
Consider this jquery statment
isTouch = document.createTouch !== undefined
我想知道我们是否在PHP中有类似的语句,不是isset(),而是从字面上检查未定义的值,例如:
I would like to know if we have a similar statement in PHP, not being isset() but literally checking for an undefined value, something like:
$isTouch != ""
PHP中是否存在与上述类似的东西?
Is there something similar as the above in PHP?
答
您可以使用-
$isTouch = isset($variable);
如果定义了$variable
,则将存储true
,否则将存储false
.如果需要反演,只需删除!
.
It will store true
if $variable
is defined else false
. If need the invers the simply remove the !
.
注意:如果var存在并且具有非NULL的值,则返回TRUE,否则返回FALSE.
Note : Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
或者如果您要检查false
,0
等,请使用empty()
-
Or if you want to check for false
, 0
etc also then use empty()
-
$isTouch = empty($variable);
empty()
适用于-
- " (一个空字符串)
- 0 (0为整数)
- 0.0 (浮点数为0)
- "0" (0为字符串)
- NULL
- 错误
- array() (一个空数组)
- $ var; (已声明变量,但没有值)
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- $var; (a variable declared, but without a value)