Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - File Handling
Identify the error in this PHP code snippet:
$file = fopen('data.txt', 'r');
fwrite($file, 'Test');
fclose($file);
AMissing file path
BFile not closed properly
CCannot write to a file opened in 'r' mode
Dfwrite syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check file open mode

    The file is opened in 'r' mode which is read-only and does not allow writing.
  2. Step 2: Understand fwrite limitation

    Calling fwrite on a read-only file causes an error because writing is not permitted.
  3. Final Answer:

    Cannot write to a file opened in 'r' mode -> Option C
  4. Quick Check:

    Write needs 'w' or 'a' mode [OK]
Quick Trick: Write only works if file opened with 'w' or 'a' [OK]
Common Mistakes:
  • Ignoring mode restrictions
  • Assuming fclose fixes write errors
  • Confusing syntax errors with mode errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes