Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q6 of 15
PHP - File Handling
Identify the error in this PHP code snippet:
$file = fopen('data.txt', 'r');
fwrite($file, "Hello");
fclose($file);
AFile path is invalid
BCannot write to a file opened in read mode
Cfwrite() syntax is incorrect
DMissing fclose() function
Step-by-Step Solution
Solution:
  1. Step 1: Check file open mode

    The file is opened in 'r' mode, which is read-only.
  2. Step 2: Understand fwrite limitation

    Writing to a file opened in read mode causes an error.
  3. Final Answer:

    Cannot write to a file opened in read mode -> Option B
  4. Quick Check:

    Write requires write mode, not read [OK]
Quick Trick: Open file in write mode to use fwrite without errors [OK]
Common Mistakes:
  • Trying to write to read-only files
  • Ignoring file open modes
  • Assuming fwrite works on any open file

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes