Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - File Handling
What will be the output of the following PHP code?
$dir = 'testdir';
if (is_dir($dir)) {
  echo 'Directory exists';
} else {
  echo 'Directory does not exist';
}
Assuming testdir exists as a directory.
ADirectory exists
BDirectory does not exist
CError: Undefined variable
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check if the directory exists using is_dir()

    Since 'testdir' exists as a directory, is_dir($dir) returns true.
  2. Step 2: Determine the output based on the condition

    The if condition is true, so it echoes 'Directory exists'.
  3. Final Answer:

    Directory exists -> Option A
  4. Quick Check:

    Directory check true = 'Directory exists' [OK]
Quick Trick: is_dir() returns true if directory exists [OK]
Common Mistakes:
  • Assuming is_dir() checks files
  • Ignoring else branch
  • Thinking it throws error if directory exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes