Bird
0
0

You want to open a file in PHP to read and write without truncating it, and create it if it doesn't exist. Which mode should you use?

hard📝 Application Q15 of 15
PHP - File Handling
You want to open a file in PHP to read and write without truncating it, and create it if it doesn't exist. Which mode should you use?
A<code>w+</code>
B<code>a+</code>
C<code>r+</code>
D<code>x+</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand each mode's behavior

    r+ reads/writes but fails if file missing; w+ truncates file; a+ reads/writes, creates if missing, appends writes; x+ creates new file only.
  2. Step 2: Match requirements

    We want read/write, no truncation, and create if missing. a+ fits all: it opens for reading and appending, creates file if missing, and does not truncate.
  3. Final Answer:

    a+ -> Option B
  4. Quick Check:

    Read/write + create + no truncate = a+ [OK]
Quick Trick: Use 'a+' to read/write and create without truncating [OK]
Common Mistakes:
  • Choosing 'r+' which fails if file missing
  • Choosing 'w+' which truncates file
  • Confusing 'x+' which fails if file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes