Bird
0
0

Consider this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - File Handling
Consider this PHP code snippet:
$dir = 'testdir';
if (mkdir($dir)) {
  echo 'Created';
} else {
  echo 'Failed';
}
rmdir($dir);

What is the main issue if the directory testdir already exists?
Amkdir() will fail and echo 'Failed', and rmdir() will fail to remove the directory
Bmkdir() will fail and echo 'Failed', but rmdir() will still remove the directory
Cmkdir() will fail and echo 'Failed', and rmdir() will remove the directory anyway
Dmkdir() will overwrite the existing directory
Step-by-Step Solution
Solution:
  1. Step 1: Behavior of mkdir() when directory exists

    If testdir already exists, mkdir() returns false and echoes 'Failed'.
  2. Step 2: Behavior of rmdir() on existing directory

    Since the directory exists, rmdir() will attempt to remove it. If the directory is empty and permissions allow, it will succeed. Otherwise, it will fail.
  3. Final Answer:

    mkdir() will fail and echo 'Failed', but rmdir() will still remove the directory -> Option B
  4. Quick Check:

    mkdir() fails if exists; rmdir() removes empty directory [OK]
Quick Trick: mkdir() fails if folder exists; rmdir() needs empty folder [OK]
Common Mistakes:
  • Assuming mkdir() overwrites existing folders
  • Expecting rmdir() to always succeed
  • Not checking if directory is empty before rmdir()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes