Re: sorting readdir output? [message #184022 is a reply to message #184021] |
Mon, 02 December 2013 21:02 |
Richard Yates
Messages: 86 Registered: September 2013
Karma:
|
Member |
|
|
On Mon, 2 Dec 2013 20:24:54 +0000 (UTC), Denis McMahon
<denismfmcmahon(at)gmail(dot)com> wrote:
> On Mon, 02 Dec 2013 10:54:49 -0800, Richard Yates wrote:
>
>> On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply(at)example(dot)com>
>> wrote:
>>
>> Isn't "false !==" unnecessary?
>>
>>> <?php
>>>
>>> if ($handle = opendir('../audio/1960/')) {
>>> echo "Directory handle: $handle\n";
>>> echo "Entries:\n";
>>>
>>> /* This is the correct way to loop over the directory. */
>>> while (false !== ($entry = readdir($handle))) {
>>> echo "$entry\n<br>";
>>> }
>>>
>>> closedir($handle);
>>> }
>>> ?>
>>>
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
>>>
>>> http://us1.php.net/readdir
>
> 1. Please post replies *BELOW* the points they address, this is usenet,
> we've been reading from down the page for years now and it's the way we'd
> like to continue doing it.
>
> 2. Your question could be easily answered by reading the relevant manual
> entry for the function concerned. Possibly much faster than by posting to
> usenet and waiting for a response. Perhaps you should try that next time?
>
> 3. Said manual entry ( http://www.php.net/manual/en/function.readdir.php
> ) contains:
>
> "Return Values
>
> Returns the entry name on success or FALSE on failure.
>
> Warning
>
> This function may return Boolean FALSE, but may also return a non-Boolean
> value which evaluates to FALSE. Please read the section on Booleans for
> more information. Use the === operator for testing the return value of
> this function."
>
> See: http://www.php.net/manual/en/language.types.boolean.php
>
> Which includes:
>
> "Converting to boolean
>
> To explicitly convert a value to boolean, use the (bool) or (boolean)
> casts. However, in most cases the cast is unnecessary, since a value will
> be automatically converted if an operator, function or control structure
> requires a boolean argument.
>
> See also Type Juggling.
>
> When converting to boolean, the following values are considered FALSE:
>
> the boolean FALSE itself
> the integer 0 (zero)
> the float 0.0 (zero)
> the empty string, and the string "0" <----------------------****
> an array with zero elements
> an object with zero member variables (PHP 4 only)
> the special type NULL (including unset variables)
> SimpleXML objects created from empty tags
>
> Every other value is considered TRUE (including any resource)."
>
> So yes, "false !==" is needed, because you can have a valid result which
> would evaluate to false (such as the valid file name "0").
(Sorry for the previous top post)
I understand the warning as it applies to many functions. Perhaps I
should have asked why there would ever be a valid result of a
directory read that evaluates to false. In practice, reading his own
directory, would he ever actually encounter a file named "0" or
"false"?
|
|
|