Bird
0
0

Consider this PHP code snippet:

medium📝 Predict Output Q5 of 15
PHP - File Handling
Consider this PHP code snippet:
$file = fopen("data.txt", "x");
fwrite($file, "Data");
fclose($file);
echo file_get_contents("data.txt");

What will happen if "data.txt" already exists?
AAn error occurs because "x" mode fails if file exists
BThe file is overwritten with "Data"
CThe file is opened for appending
DThe file is opened for reading only
Step-by-Step Solution
Solution:
  1. Step 1: Understand "x" mode behavior

    "x" mode creates a new file and fails if the file exists.
  2. Step 2: Check what happens if file exists

    Since "data.txt" exists, fopen returns false and triggers an error.
  3. Final Answer:

    An error occurs because "x" mode fails if file exists -> Option A
  4. Quick Check:

    "x" mode = create new only, error if exists [OK]
Quick Trick: "x" fails if file exists, use "w" to overwrite [OK]
Common Mistakes:
  • Thinking "x" overwrites existing files
  • Confusing "x" with "a" append mode
  • Assuming "x" opens for reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes