Bird
0
0

You want to list all files in a directory docs but skip the special entries . and ... Which PHP code snippet correctly does this?

hard📝 Application Q15 of 15
PHP - File Handling
You want to list all files in a directory docs but skip the special entries . and ... Which PHP code snippet correctly does this?
Aif (is_dir('docs')) { $dh = opendir('docs'); while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { echo $file . ' '; } } closedir($dh); }
Bif (is_dir('docs')) { $dh = opendir('docs'); while (($file = readdir($dh))) { echo $file . ' '; } closedir($dh); }
Cforeach (scandir('docs') as $file) { echo $file . ' '; }
Dif (is_dir('docs')) { $dh = opendir('docs'); while (($file = readdir($dh)) !== false) { echo $file . ' '; } closedir($dh); }
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to skip '.' and '..'

    The special entries '.' and '..' appear in directory listings and must be explicitly skipped.
  2. Step 2: Analyze each code snippet

    if (is_dir('docs')) { $dh = opendir('docs'); while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { echo $file . ' '; } } closedir($dh); } checks if the file is not '.' or '..' before echoing. foreach (scandir('docs') as $file) { echo $file . ' '; } uses scandir() but does not skip special entries.
  3. Final Answer:

    The snippet using if ($file != '.' && $file != '..') -> Option A
  4. Quick Check:

    Skip '.' and '..' with if check inside readdir() loop [OK]
Quick Trick: Use if ($file != '.' && $file != '..') to skip special entries [OK]
Common Mistakes:
  • Not skipping '.' and '..' entries
  • Using scandir() without filtering special entries
  • Forgetting to check for false in readdir() loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes