Php/keyword empty
来自菜鸟教程
PHP空关键字
例
检查变量是否为空:
<?php
$str = "";
if(empty($str)) {
echo "The string is
empty";
}
?>
定义和用法
The
empty
关键字充当一个函数,如果变量不存在或者其值被认为是空的,则返回true。The
empty
关键字还计算不在变量中的表达式。
如果值是以下任意值,则将其视为空:
- 空字符串
- 空数组
- 整数0
- 浮点数0.0
- 字符串“ 0”
- 布尔值false
- null
更多例子
例
Use
empty
在各种不同的表达方式上:
<?php
// A variable that does not exist
if(empty($x)) {
echo '$x does not exist<br>';
}
// An empty integer
if(empty(0)) {
echo '0 is empty<br>';
}
// An empty float
if(empty(0.0)) {
echo '0.0 is empty<br>';
}
// An empty string
if(empty("")) {
echo '"" is an empty string<br>';
}
// null
if(empty(null)) {
echo 'null is empty<br>';
}
// A value that is not empty
if(empty('A')) {
echo '"A" is empty<br>';
} else {
echo
'"A" is not empty<br>';
}
?>