Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - File Handling
What will be the output of this PHP code?
$file = fopen('output.txt', 'w');
fwrite($file, 'Hello');
fwrite($file, ' World');
fclose($file);
echo file_get_contents('output.txt');
AHello World
BHello
C World
DError: File not closed
Step-by-Step Solution
Solution:
  1. Step 1: Trace fwrite calls

    First fwrite writes 'Hello', second appends ' World' to the same file handle.
  2. Step 2: Confirm file closure and reading

    fclose closes file properly, file_get_contents reads full content 'Hello World'.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    Multiple fwrite calls append to file handle [OK]
Quick Trick: Multiple fwrite calls add data sequentially [OK]
Common Mistakes:
  • Expecting only last fwrite content
  • Not closing file before reading
  • Confusing fwrite with file_put_contents

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes