温馨提示×

php如何判断字符串是否为空

PHP
小亿
103
2024-04-09 15:38:31
栏目: 编程语言
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在PHP中,可以使用empty()函数和strlen()函数来判断字符串是否为空。

  1. 使用empty()函数:
$str = "hello";
if (empty($str)) {
    echo "String is empty";
} else {
    echo "String is not empty";
}

如果字符串为空,empty()函数会返回true,否则返回false

  1. 使用strlen()函数:
$str = "hello";
if (strlen($str) == 0) {
    echo "String is empty";
} else {
    echo "String is not empty";
}

strlen()函数会返回字符串的长度,如果长度为0,则字符串为空。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:php怎么判断字符串是否为空

0