Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - File Handling
What will be the output of this PHP code?
$file = fopen('test.txt', 'a');
fwrite($file, "Hello\n");
fclose($file);
echo file_get_contents('test.txt');
AHello
BEmpty output
CError: Cannot write to file
DolleH
Step-by-Step Solution
Solution:
  1. Step 1: Understand the 'a' mode effect

    Mode 'a' opens the file for appending, so "Hello\n" (string with actual newline) is added at the end.
  2. Step 2: Check output of file_get_contents

    It reads the whole file content including the newline, and echo prints it with the newline interpreted.
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    "Hello\n" adds a newline, so output is Hello followed by newline [OK]
Quick Trick: Append adds 'Hello\n'; echo shows Hello with newline [OK]
Common Mistakes:
  • Thinking '\n' becomes literal backslash-n
  • Expecting 'Hello' without newline
  • Assuming error without write permission

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes