Home  >  Article  >  Backend Development  >  Why does the PHP readdir function have different results every time it is executed?

Why does the PHP readdir function have different results every time it is executed?

WBOY
WBOYOriginal
2016-08-04 09:20:59962browse

<code><?php 
    $path = dirname(__FILE__);
    $dir = opendir($path);
    while($file = readdir($dir))
    {
        echo "$file\n";
    }</code>

The code is as above:
There are two execution results.
The first kind
Why does the PHP readdir function have different results every time it is executed?
The second kind

Why does the PHP readdir function have different results every time it is executed?
Under win7 environment, can this function of php 5.6 be executed normally in linux environment?

Reply content:

<code><?php 
    $path = dirname(__FILE__);
    $dir = opendir($path);
    while($file = readdir($dir))
    {
        echo "$file\n";
    }</code>

The code is as above:
There are two execution results.
The first kind
Why does the PHP readdir function have different results every time it is executed?
The second kind

Why does the PHP readdir function have different results every time it is executed?
Under win7 environment, can this function of php 5.6 be executed normally in linux environment?

Refer to php official documentation

<code>$path = dirname(__FILE__);
$dir = opendir($path);
while(false!==($file = readdir($dir)))
{
    echo "$file\n";
}</code>

This is the correct way you should write it, with official documentation attached

<code>使用readdir函数成功则返回文件名 或者在失败时返回 FALSE

</code>

But you should be especially careful::Warning

<code>此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。
请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。
</code>

See if you have a file or directory named 0?

It’s a permission issue... The process user should be PHP or Apache, not your login account.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn