Jump to content

Why are these not the same?


SaranacLake

Recommended Posts

Why does this code seem to run okay...

$directory = "images/";

// Open a directory, and read its contents
if (is_dir($directory)){
  if ($opendirectory = opendir($directory)){
    while (($file = readdir($opendirectory)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($opendirectory);
  }
}
	

 

And this code sends my computer into an infinite loop...

$directory = "images/";

if (is_dir($directory)){
	$opendirectory = opendir($directory);
    
    if($opendirectory){
    	$file = readdir($opendirectory);
        
        while($file !== FALSE){
        	echo $file . "<br>";
		}
        
        closedir($opendirectory);
	}
}

 

 

Edited by SaranacLake
Link to comment
Share on other sites

8 minutes ago, maxxd said:

In the second code block, you're only running the readdir() once, so as long as it's not false on the initial run, it never will be.

That's why you are a "guru"!!  😉

 

Is there a cleaner way to write this...

	while (($file = readdir($opendirectory)) !== false){
	

 

As you can see, I tried to break that up into two parts, because I hate the idea of doing an *assignment* and a *logical comparison* all in one statement.  Unfortunately I broke the logic as you pointed out.

Link to comment
Share on other sites

2 hours ago, SaranacLake said:

I hate the idea of doing an *assignment* and a *logical comparison* all in one statement

I totally get that, but there's really not a more elegant way to handle this situation that I can think of off the top of my head - maybe someone else can pop in with a suggestion.

Link to comment
Share on other sites

5 hours ago, SaranacLake said:

!== FALSE)

Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr).

Link to comment
Share on other sites

33 minutes ago, benanamen said:

As with if, while is by default a truthy check so you simply just need to do while (expr)

The point of explicitly checking for false is usually to avoid confusion.  For example if there were a file named "0" then the loop would incorrectly stop at that file.

Link to comment
Share on other sites

9 hours ago, benanamen said:

Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr).

!== does not just mean "not TRUE" - It means "not identical".

Now where is the "lack of understanding"?  😉

https://www.php.net/manual/en/function.readdir.php   (see the "Warning" section.)

https://www.php.net/maunal/en/lanuage.operators.comparison.php

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.