Re: readdir() question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, August 15, 2006 12:04 pm, John Meyer wrote:
> I have a script to list the files in a directory:
>
> <select name="letters">
> <?php
> 	$open = opendir(".");
> 	while ($file = readdir($open) != false) {
> ?>
> 	<option value="<?=$file?>"><?=$file?></option>
> <?php
> 	}
> ?>
> </select>
> </form>
>
> And all I am getting are "1"s.  I think I'm doing it right, what is
> the
> disconnect?

It's not a readdir question.  It's an Order of Operations question.
:-)

$file = readdir($open) != false

You probably believe that PHP is going to magically "know" that you
want this bit:
        readdir($open) != false
to be done "first"

But PHP can't read your mind.

It's going to look at the facts of the case.

= and != have equal priority in PHP,
and in case of a "tie" it will evaluate them left-to-right:

$file = readdir($open)

This gives you the name of the file.

PHP then does the != false bit, comparing the name of a file with false.

Unless your filename starts with one or more '0' characters, and then
has alpha characters for the first non-numeric characters after the
'0's, then it ain't gonna be equal to false, and it will always return
1.

'afile' != false ----> 1
'filename' != false -> 1
'000name' != false --> 0
'012name' != false --> 1

You really do need to put the parentheses in there, so PHP does things
in the rigth order, just like the manual says to:
http://php.net/readdir

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux