According to information on https://www.php.net/manual/en/function.readdir.php, the readdir()
function
may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
and accordingly advises using ===
for testing.
What possible non-boolean value might this be? In my testing, I can only get a false false
, as it were, in the following example:
$dirname='0'; // misinterpreted
print $dirname==false?'false':'true';
All other strings are interpreted as true
.
Are there any other strings which would evaluate to false? Or is there some other return result which would evaluate as false?
Are there any other strings which would evaluate to false? Or is there some other return result which would evaluate as false?
The manual for the Boolean type lists all values that get considered 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
And readdir
[r]eturns the entry name on success or FALSE on failure
- so unless you are getting false, the value should always be a string. (Directory entries are always considered string values, on that level the concept of a “number” does not exist.)