Bird
0
0

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

easy📝 Syntax Q3 of 15
PHP - File Handling
Which of the following is the correct syntax to open a file for reading in PHP?
A$fp = fopen('file.txt', 'r');
B$fp = fopen('file.txt', 'w');
C$fp = fopen('file.txt', 'rw');
D$fp = fopen('file.txt', 'a');
Step-by-Step Solution
Solution:
  1. Step 1: Understand fopen modes

    'r' opens file for reading only, 'w' for writing (truncates), 'rw' is invalid, 'a' for appending.
  2. Step 2: Identify correct mode for reading

    Only 'r' mode opens file for reading without modifying it.
  3. Final Answer:

    $fp = fopen('file.txt', 'r'); -> Option A
  4. Quick Check:

    Open file for reading = 'r' mode [OK]
Quick Trick: Use 'r' mode in fopen() to open file for reading [OK]
Common Mistakes:
  • Using 'w' which truncates file
  • Using invalid mode 'rw'
  • Confusing 'a' (append) with read mode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes