Bird
0
0

Find the bug in this PHP code:

medium📝 Debug Q7 of 15
PHP - File Handling
Find the bug in this PHP code:
$file = 'report.pdf';
if (is_dir($file)) {
    echo 'It is a directory';
} else {
    echo 'It is not a directory';
}
AVariable $file should be a directory path, not a file
BNo bug, code works correctly
Cis_dir() cannot be used with variables
DEcho statements are missing semicolons
Step-by-Step Solution
Solution:
  1. Step 1: Understand is_dir() usage

    is_dir() checks if the given path is a directory.
  2. Step 2: Analyze variable content

    $file contains a filename, not a directory path, so is_dir() will return false.
  3. Final Answer:

    $file should be a directory path, not a file -> Option A
  4. Quick Check:

    is_dir() expects directory path, not file name [OK]
Quick Trick: Use is_dir() only with directory paths [OK]
Common Mistakes:
  • Using is_dir() on files
  • Assuming variables can't be used
  • Missing semicolons in echo

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes