PHP readdir() 并返回一个虚假值
PHP readdir() and returning a falsy value
根据 https://www.php.net/manual/en/function.readdir.php 上的信息,readdir()
函数
may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
因此建议使用 ===
进行测试。
这可能是什么非布尔值?在我的测试中,我只能得到一个错误的 false
,就像下面的例子:
$dirname='0'; // misinterpreted
print $dirname==false?'false':'true';
所有其他字符串都被解释为 true
。
是否有任何其他字符串的计算结果为 false?或者是否有其他 return 结果会被评估为 false?
Are there any other strings which would evaluate to false? Or is there some other return result which would evaluate as false?
布尔类型的手册列出了所有被视为假的值,https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting:
When converting to boolean, the following values are considered FALSE:
- the boolean FALSE itself
- the integers 0 and -0 (zero)
- the floats 0.0 and -0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- the special type NULL (including unset variables)
- SimpleXML objects created from empty tags
[r]eturns the entry name on success or FALSE on failure
- 所以除非你得到假,否则该值应该始终是一个字符串。 (目录条目始终被视为字符串值,在该级别上不存在“数字”的概念。)
根据 https://www.php.net/manual/en/function.readdir.php 上的信息,readdir()
函数
may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
因此建议使用 ===
进行测试。
这可能是什么非布尔值?在我的测试中,我只能得到一个错误的 false
,就像下面的例子:
$dirname='0'; // misinterpreted
print $dirname==false?'false':'true';
所有其他字符串都被解释为 true
。
是否有任何其他字符串的计算结果为 false?或者是否有其他 return 结果会被评估为 false?
Are there any other strings which would evaluate to false? Or is there some other return result which would evaluate as false?
布尔类型的手册列出了所有被视为假的值,https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting:
When converting to boolean, the following values are considered FALSE:
- the boolean FALSE itself
- the integers 0 and -0 (zero)
- the floats 0.0 and -0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- the special type NULL (including unset variables)
- SimpleXML objects created from empty tags
[r]eturns the entry name on success or FALSE on failure
- 所以除非你得到假,否则该值应该始终是一个字符串。 (目录条目始终被视为字符串值,在该级别上不存在“数字”的概念。)