Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - File Handling
What will be the output of this PHP code?
$file = fopen("test.txt", "w");
fwrite($file, "Hello");
fclose($file);
$file = fopen("test.txt", "a");
fwrite($file, " World");
fclose($file);
$content = file_get_contents("test.txt");
echo $content;
AHello World
BHello
C World
DError: Cannot open file
Step-by-Step Solution
Solution:
  1. Step 1: Write "Hello" with "w" mode

    Opening with "w" clears file and writes "Hello".
  2. Step 2: Append " World" with "a" mode

    Opening with "a" adds " World" at the end without erasing.
  3. Step 3: Read and output file content

    file_get_contents reads full content: "Hello World".
  4. Final Answer:

    The output is "Hello World" -> Option A
  5. Quick Check:

    Write then append = combined text [OK]
Quick Trick: "w" writes fresh, "a" adds after existing content [OK]
Common Mistakes:
  • Assuming "a" overwrites instead of appends
  • Forgetting "w" clears file first
  • Expecting error on file open

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes