Home > Backend Development > PHP Tutorial > 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
Release: 2016-08-04 09:20:59
Original
1029 people have browsed it

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

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>
Copy after login
Copy after login

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>
Copy after login

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

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

</code>
Copy after login

But you should be especially careful::Warning

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

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.

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template