Bird
0
0

You want to append a new line "New entry" to an existing file "log.txt" without deleting its current content. Which PHP code snippet correctly does this?

hard📝 Application Q15 of 15
PHP - File Handling
You want to append a new line "New entry" to an existing file "log.txt" without deleting its current content. Which PHP code snippet correctly does this?
A$file = fopen('log.txt', 'a'); fwrite($file, "New entry\n"); fclose($file);
B$file = fopen('log.txt', 'w'); fwrite($file, "New entry\n"); fclose($file);
C$file = fopen('log.txt', 'r'); fwrite($file, "New entry\n"); fclose($file);
D$file = fopen('log.txt', 'x'); fwrite($file, "New entry\n"); fclose($file);
Step-by-Step Solution
Solution:
  1. Step 1: Understand file open modes for appending

    Mode 'a' opens the file for writing at the end, preserving existing content.
  2. Step 2: Check other modes' effects

    'w' overwrites the file, 'r' is read-only, 'x' creates new file and fails if exists.
  3. Final Answer:

    $file = fopen('log.txt', 'a'); fwrite($file, "New entry\n"); fclose($file); -> Option A
  4. Quick Check:

    Append mode = 'a' [OK]
Quick Trick: Use 'a' mode to add without erasing file content [OK]
Common Mistakes:
  • Using 'w' mode which erases existing content
  • Trying to write in 'r' mode causing errors
  • Using 'x' mode which fails if file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes