php 检查字符串是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/718986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:38:29  来源:igfitidea点击:

Checking if the string is empty

phpstringvalidation

提问by bgosalci

I have a function isNotEmpty which returns true if the string is not empty and false if the string is empty. I've found out that it is not working if I pass an empty string through it.

我有一个函数 isNotEmpty 如果字符串不为空则返回 true ,如果字符串为空则返回 false 。我发现如果我通过它传递一个空字符串它不起作用。

function isNotEmpty($input) 
{
    $strTemp = $input;
    $strTemp = trim($strTemp);

    if(strTemp != '') //Also tried this "if(strlen($strTemp) > 0)"
    {
         return true;
    }

    return false;
}

The validation of the string using isNotEmpty is done:

使用 isNotEmpty 验证字符串已完成:

if(isNotEmpty($userinput['phoneNumber']))
{
    //validate the phone number
}
else
{
    echo "Phone number not entered<br/>";
}

If the string is empty the else doesn't execute, I don't understand why, can someone please shed some light on this please.

如果字符串为空,则 else 不执行,我不明白为什么,请有人对此有所了解。

回答by cletus

Simple problem actually. Change:

其实很简单的问题。改变:

if (strTemp != '')

to

if ($strTemp != '')

Arguably you may also want to change it to:

可以说,您可能还想将其更改为:

if ($strTemp !== '')

since != ''will return true if you pass is numeric 0 and a few other cases due to PHP's automatic type conversion.

因为!= ''如果您传递的是数字 0 和其他一些由于PHP 的自动类型转换的情况,则将返回 true 。

You should notuse the built-in empty()function for this; see comments and the PHP type comparison tables.

不应该为此使用内置的empty()函数;请参阅注释和PHP 类型比较表

回答by Malak

PHP have a built in function called empty()the test is done by typing if(empty($string)){...}Reference php.net : php empty

PHP 有一个称为empty()测试的内置函数,通过键入 if(empty($string)){...}Reference php.net : php empty 来完成

回答by Dexygen

I always use a regular expression for checking for an empty string, dating back to CGI/Perl days, and also with Javascript, so why not with PHP as well, e.g. (albeit untested)

我总是使用正则表达式来检查空字符串,可以追溯到 CGI/Perl 时代,也使用 Javascript,那么为什么不使用 PHP,例如(尽管未经测试)

return preg_match('/\S/', $input);

Where \S represents any non-whitespace character

\S 代表任何非空白字符

回答by Bj?rn

In your ifclause in the function, you're referring to a variable strTempthat doesn't exist. $strTempdoes exist, though.

if函数的子句中,您指的是一个strTemp不存在的变量。$strTemp不过确实存在。

But PHP already has an empty()function available; why make your own?

但是 PHP 已经有一个empty()可用的函数了;为什么要自己做?

if (empty($str))
    /* String is empty */
else
    /* Not empty */

From php.net:

来自 php.net:

Return Values

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

返回值

如果 var 具有非空和非零值,则返回 FALSE。

以下内容被认为是空的:

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

http://www.php.net/empty

http://www.php.net/empty

回答by troelskn

PHP evaluates an empty string to false, so you can simply use:

PHP 将空字符串评估为 false,因此您可以简单地使用:

if (trim($userinput['phoneNumber'])) {
  // validate the phone number
} else {
  echo "Phone number not entered<br/>";
}

回答by doctorlai

Just use strlen() function

只需使用 strlen() 函数

if (strlen($s)) {
   // not empty
}

回答by svarog

I just write my own function, is_stringfor type checking and strlento check the length.

我只是编写了自己的函数,用于类型检查的is_string和用于检查长度的strlen

function emptyStr($str) {
    return is_string($str) && strlen($str) === 0;
}

print emptyStr('') ? "empty" : "not empty";
// empty

Here's a small test repl.it

这是一个小测试 repl.it

EDIT: You can also use the trimfunction to test if the string is also blank.

编辑:您还可以使用修剪功能来测试字符串是否也为空。

is_string($str) && strlen(trim($str)) === 0;    

回答by TRayman

I needed to test for an empty field in PHP and used

我需要在 PHP 中测试一个空字段并使用

ctype_space($tempVariable)

which worked well for me.

这对我来说效果很好。

回答by abhishek bagul

Well here is the short method to check whether the string is empty or not.

那么这里是检查字符串是否为空的简短方法。

$input; //Assuming to be the string


if(strlen($input)==0){
return false;//if the string is empty
}
else{
return true; //if the string is not empty
}

回答by Fabian Picone

You can simply cast to bool, dont forget to handle zero.

您可以简单地强制转换为 bool,不要忘记处理零。

function isEmpty(string $string): bool {
    if($string === '0') {
        return false;
    }
    return !(bool)$string;
}

var_dump(isEmpty('')); // bool(true)
var_dump(isEmpty('foo')); // bool(false)
var_dump(isEmpty('0')); // bool(false)