Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - File Handling
What will be the output of the following PHP code?
$dir = 'myfolder';
if (is_dir($dir)) {
  $handle = opendir($dir);
  while (($file = readdir($handle)) !== false) {
    echo $file . ' ';
  }
  closedir($handle);
} else {
  echo 'Directory not found';
}
ASyntax error
BOnly files inside 'myfolder' without '.' and '..'
CDirectory not found
DList of all files and folders inside 'myfolder' including '.' and '..'
Step-by-Step Solution
Solution:
  1. Step 1: Check if directory exists

    The code uses is_dir() to check if 'myfolder' exists. If it does, it proceeds to open it.
  2. Step 2: Read directory contents with readdir()

    The readdir() function reads all entries including special entries '.' (current directory) and '..' (parent directory). These are echoed with spaces.
  3. Final Answer:

    List of all files and folders inside 'myfolder' including '.' and '..' -> Option D
  4. Quick Check:

    readdir() lists all entries including '.' and '..' [OK]
Quick Trick: readdir() returns '.' and '..' entries by default [OK]
Common Mistakes:
  • Assuming '.' and '..' are excluded automatically
  • Not checking if directory exists before reading
  • Forgetting to close directory handle

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes