Bird
0
0

What will be the output of the following PHP code?

medium📝 Predict Output Q13 of 15
PHP - File Handling
What will be the output of the following PHP code?
$file = fopen('test.txt', 'w');
fwrite($file, "Hello World");
fclose($file);
echo file_get_contents('test.txt');
AError: file not found
Btest.txt
CHello World
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze file opening and writing

    The file 'test.txt' is opened in write mode, then "Hello World" is written to it, and the file is closed.
  2. Step 2: Read and output file contents

    file_get_contents reads the file content, which now contains "Hello World", so echo prints it.
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    Write then read file = Hello World [OK]
Quick Trick: Write then read file outputs written text [OK]
Common Mistakes:
  • Assuming file_get_contents returns filename instead of content
  • Forgetting to close the file before reading
  • Expecting error if file does not exist (it is created in write mode)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes