empty

创建于 2024-12-03 / 53
字体: [默认] [大] [更大]

❮ PHP 关键字

实例

检查变量是否为空:

<?php
$str = "";
if(empty($str)) {
  echo "The string is empty";
}
?> 亲自试一试 »

定义和用法

empty 关键字充当一个函数,如果变量不存在,或者它的值被视为空,则返回 true。 empty 关键字还计算不在变量中的表达式。

如果值是以下任何一种,则将其视为空值:

  • 一个空字符串
  • 一个空数组
  • 整数0
  • 浮点数0.0
  • 字符串"0"
  • 布尔假

更多实例

实例

在各种不同的表达式上使用 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>';
}
?> 亲自试一试 »
❮ PHP 关键字
0 人点赞过