Bird
0
0

Which of the following is the correct way to open a CSV file for writing in PHP?

easy📝 Syntax Q12 of 15
PHP - File Handling
Which of the following is the correct way to open a CSV file for writing in PHP?
A$file = fopen('data.csv', 'r');
B$file = fopen('data.csv', 'rw');
C$file = fopen('data.csv', 'w');
D$file = fopen('data.csv', 'x');
Step-by-Step Solution
Solution:
  1. Step 1: Understand fopen modes

    Mode 'w' opens a file for writing and truncates it if it exists, perfect for writing CSV files.
  2. Step 2: Check other modes

    'r' is read-only, 'rw' is invalid, 'x' creates a new file but fails if it exists.
  3. Final Answer:

    $file = fopen('data.csv', 'w'); -> Option C
  4. Quick Check:

    Open file for writing = 'w' mode [OK]
Quick Trick: Use 'w' mode in fopen() to write files [OK]
Common Mistakes:
  • Using 'r' mode when intending to write
  • Using invalid mode 'rw'
  • Using 'x' which fails if file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes