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 = fopen('notes.txt', 'w');
fwrite($file, "Note 1");
// forgot fclose
echo filesize('notes.txt');
Aecho should be before fwrite
Bfilesize returns 0 because file not closed
Cfopen mode incorrect
Dfwrite syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Notice missing fclose()

    The file is not closed after writing, so data may be buffered.
  2. Step 2: Understand filesize behavior

    filesize returns 0 because the file system hasn't updated the file size yet.
  3. Final Answer:

    filesize returns 0 because file not closed -> Option B
  4. Quick Check:

    Missing fclose causes filesize = 0 [OK]
Quick Trick: Always fclose before filesize to get correct size [OK]
Common Mistakes:
  • Forgetting to close files after writing
  • Expecting immediate filesize update
  • Misunderstanding fwrite buffering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes