Bird
0
0

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

easy📝 Syntax Q12 of 15
PHP - File Handling
Which of the following is the correct syntax to open a file for writing in PHP?
A<code>fopen('file.txt', 'r')</code>
B<code>fopen('file.txt', 'a')</code>
C<code>fopen('file.txt', 'rw')</code>
D<code>fopen('file.txt', 'w')</code>
Step-by-Step Solution
Solution:
  1. Step 1: Recall the mode for writing

    The mode 'w' opens a file for writing and truncates it if it exists.
  2. Step 2: Check syntax correctness

    fopen('file.txt', 'w') is the correct syntax to open for writing.
  3. Final Answer:

    fopen('file.txt', 'w') -> Option D
  4. Quick Check:

    Write mode = 'w' [OK]
Quick Trick: Use 'w' to open file for writing in fopen [OK]
Common Mistakes:
  • Using 'r' which is read-only
  • Using 'rw' which is invalid in PHP
  • Confusing 'a' (append) with write

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes