溫馨提示×

php如何判斷字符串是否為空

PHP
小億
97
2024-04-09 15:38:31
欄目: 編程語言

在PHP中,可以使用empty()函數(shù)和strlen()函數(shù)來判斷字符串是否為空。

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

如果字符串為空,empty()函數(shù)會返回true,否則返回false。

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

strlen()函數(shù)會返回字符串的長度,如果長度為0,則字符串為空。

0