<?php while ($row=$dir->read())  ?>
// will break as soon as $row can be casted to false, which is the case for a directory named "0"
<?php while (false !== ($row=$dir->read())) ?>
// will do the job as expected(PHP 4, PHP 5, PHP 7, PHP 8)
Directory::read — ディレクトリハンドルからエントリを読み込む
| バージョン | 説明 | 
|---|---|
| 8.0.0 | 引数を取らなくなりました。 これより前のバージョンでは、 ディレクトリハンドルを引数に渡すことができました。 | 
<?php while ($row=$dir->read())  ?>
// will break as soon as $row can be casted to false, which is the case for a directory named "0"
<?php while (false !== ($row=$dir->read())) ?>
// will do the job as expecteduse 
while ($row=$dir->read()){
// your code
} 
to get a list of all files and directories. You can definitely use the "for" and other loops.