strlen()== 0和empty()之间有区别吗?

red*_*d4d 8 php strlen

我正在查看其他人写过的表格验证码,我看到了这个:

strlen() == 0
Run Code Online (Sandbox Code Playgroud)

在测试表单变量是否为空时,我使用该empty()函数.有一种方式比另一种更好吗?它们在功能上是否相同?

Pau*_*aul 8

有几种情况会有不同的行为:

empty('0'); // returns true, 
strlen('0'); // returns 1.

empty(array()); // returns true,
strlen(array()); // returns null with a Warning in PHP>=5.3 OR 5 with a Notice in PHP<5.3.

empty(0); // returns true,
strlen(0); // returns 1.
Run Code Online (Sandbox Code Playgroud)


Ham*_*han 6

以下内容被认为是空的:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Run Code Online (Sandbox Code Playgroud)

strlen()只是检查字符串len是否为0.它不检查int,float等.你的情况如何.

参考


dpp*_*dpp 5

strlen是用于获取字符串中的字符数,同时empty用于测试变量是否为空

空的含义:

empty("") //is empty for string
empty(0) // is empty for numeric types
empty(null) //is empty 
empty(false) //is empty for boolean
Run Code Online (Sandbox Code Playgroud)