Bird
0
0

You want to open a file for reading and writing without truncating it, and create it if it doesn't exist. Which mode should you use?

hard📝 Application Q8 of 15
PHP - File Handling
You want to open a file for reading and writing without truncating it, and create it if it doesn't exist. Which mode should you use?
A"r+"
B"a+"
C"w+"
D"x+"
Step-by-Step Solution
Solution:
  1. Step 1: Analyze each mode

    "r+" requires existing file, no create; "w+" truncates file; "a+" reads/writes, creates if missing, no truncation; "x+" creates new only.
  2. Step 2: Choose mode matching requirements

    "a+" fits: read/write, create if missing, no truncation.
  3. Final Answer:

    Use "a+" mode for read/write, create, no truncate -> Option B
  4. Quick Check:

    Read/write + create + no truncate = "a+" [OK]
Quick Trick: "a+" reads/writes, creates file, no erase [OK]
Common Mistakes:
  • Choosing "r+" which fails if file missing
  • Using "w+" which clears file
  • Thinking "x+" opens existing files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes