Do not understand readdir() function

Discussion in 'PHP' started by Tom12361, Aug 17, 2008.

Thread Status:
Not open for further replies.
  1. #1
    Here is a code:

    <?php
    $directory = "lala";
    $d = opendir($directory) or die ("couldn't access $directory");
    while(!($file = readdir($d)) === false){
    echo $file."<BR>";
    }
    ?>
    PHP:
    Don't understand this place:

    while(!($file = readdir($d)) === false)
    PHP:
    Can somebody explain? Thank you very much.
     
    Tom12361, Aug 17, 2008 IP
  2. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #2
    readdir() "Returns the filename on success, or FALSE on failure. "

    while(!($file = readdir($d)) === false){
    echo $file."<BR>";
    Code (markup):
    this means keep printing out the file names till there are no more,
    (until readdir() returns false)
     
    Jezz, Aug 17, 2008 IP
  3. Tom12361

    Tom12361 Active Member

    Messages:
    442
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I don't understand why is while(!($file = readdir($d)) === false), but why is not while(!($file = readdir($d)) === true)
     
    Tom12361, Aug 17, 2008 IP
  4. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    while(!($file = readdir($d)) === false)
    Code (markup):
    Translates to:

    while(Not($file) is exactly equal too false)

    (false being boolean 0, not a string)

    ! means not,
    and === means exactly equal (the same value and the same type)

    == would work fine unless there was a file named "false" or "0".
     
    Jezz, Aug 17, 2008 IP
  5. Tom12361

    Tom12361 Active Member

    Messages:
    442
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Oh, now I've understood, thank you very much, Jezz. :)
     
    Tom12361, Aug 17, 2008 IP
  6. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #6
    No problem Tom
    Glad I could help :)
     
    Jezz, Aug 17, 2008 IP
  7. Tom12361

    Tom12361 Active Member

    Messages:
    442
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Sorry, one more question, why doesnt work this code:

    <?php
    $directory = "lala";
    $d = opendir($directory) or die ("couldn't access $directory");
    while($file = readdir($d) === true){
    echo $file."<BR>";
    }
    ?>
    PHP:
     
    Tom12361, Aug 17, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Because readdir() does NOT return true... never.
     
    nico_swd, Aug 17, 2008 IP
  9. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #9
    because you are saying:
    while($file === true)
    and $file equals the name of the file it's reading, or false when there are no more files.
    It never equals true, so the code in the loop will never execute.

    Edit: posted @ the same time nico_swd :)
     
    Jezz, Aug 17, 2008 IP
Thread Status:
Not open for further replies.