Bird
0
0

What is wrong with this PHP code snippet?

medium📝 Debug Q7 of 15
PHP - File Handling
What is wrong with this PHP code snippet?
$file = fopen("output.txt", "a+");
fread($file, 100);
fwrite($file, "More data");
fclose($file);
ACannot read and write with "a+" mode
BMissing file open mode
Cfwrite must come before fread
DFile pointer is at the end, so fread reads nothing
Step-by-Step Solution
Solution:
  1. Step 1: Understand "a+" mode pointer position

    "a+" opens for reading and writing but pointer starts at file end.
  2. Step 2: Effect on fread

    Since pointer is at end, fread reads zero bytes (nothing).
  3. Final Answer:

    fread reads nothing because pointer is at file end -> Option D
  4. Quick Check:

    "a+" pointer at end, fread reads zero [OK]
Quick Trick: "a+" starts pointer at end; rewind before reading [OK]
Common Mistakes:
  • Thinking "a+" disallows reading
  • Assuming fread reads from start automatically
  • Confusing order of fread and fwrite

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes