0

I am new to php and it's my first language. So my question seems to be very basic.

First the basic concept of a while loop seems pretty clear to me. As long as i increment (i++) until the "crash condition" (i<10) becomes true.

But I realy wonder why the following code example without any explicit incrementation works.

$originals = "originale";
$thumbnails="thumbnails";
$directory = opendir($originale);
$images = array();
while(($file = readdir($directory)) !== false) {
    if(preg_match("/\.jp?g$/i", $file)){
        $images[] = $file;
    }
}
echo "<pre>";
var_dump($images);
echo "</pre>";
closedir($directory);

Obviously I missed something. It seems to be magic. I would expect an enless loop which returns the first file... ?

I hope that despite my English (sorry for that) the question becomes clear. Many thanks in advance.

5
  • 2
    while-loops run while the condition inside their braces is true. the condition in this case is ($file = readdir($directory)), which tries to assign a new filename from the directory to $file and - as a whole - returns false if there is no new file available. but: explanation of basic code is quite off-topic for SO - this is not a tutorial site. [search engine of your choice] provides quite a few which should be better suited for your needs. Commented Oct 24, 2016 at 9:22
  • 1
    Because while loop's doesn't need to have a incremental value, as long as there is something to break the loop, in this case it's when $file is set to false. Commented Oct 24, 2016 at 9:22
  • 1
    readdir will advance the pointer, and if pointer reach the end of directory, it will return false. Commented Oct 24, 2016 at 9:22
  • @NiyokoYuliawan: Ah ok, i suspected something like that. Wondering why german php-manual doesn't mention it. It semms to be too obvious. The more i thank you for your impulse. Seems similar to the construct of fetch in context of mysqli, which was also magic for me. Commented Oct 24, 2016 at 9:28
  • @Franz Thank you. I have three beginner books in front of me and they don't close the bridge from beginners example to "internal pointers" and read the manual. Sorry if I went out of topic, you helped me a lot. Commented Oct 24, 2016 at 9:41

1 Answer 1

2

As you can see in the PHP documentation readdir does:

Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.

So everytime you call readdir will return the next file from that directory.

For example if you have a direcotry with 2 files:

  • one.txt
  • two.txt

First time you call readdir will return one.txt. And this is different from false so will execute the code inside the while

Second time you call readdir will return two.txt and the same as above.

Thrid time you call readdir will return false because no more files are in that dirrecotry. and the condition from while will not be true anymore so will not enter while anymore.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! Although I read the manual, I wasn't able to go over that bridge. Now that you give me the context it's plausible. I was confronted with similar concept several times. Now it seems that you bring me much closer to the point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.