Bird
0
0

Which of the following is the correct way to write the string "Goodbye" to a file named "farewell.txt" using fwrite() in PHP?

easy📝 Syntax Q3 of 15
PHP - File Handling
Which of the following is the correct way to write the string "Goodbye" to a file named "farewell.txt" using fwrite() in PHP?
Afile_put_contents('farewell.txt', 'Goodbye');
Bfwrite('farewell.txt', 'Goodbye'); fclose('farewell.txt');
C$file = fopen('farewell.txt', 'w'); fwrite($file, 'Goodbye'); fclose($file);
D$file = fopen('farewell.txt', 'r'); fwrite($file, 'Goodbye'); fclose($file);
Step-by-Step Solution
Solution:
  1. Step 1: Open file in write mode

    Use fopen() with 'w' to open for writing.
  2. Step 2: Write string to file

    Use fwrite() with the file handle and string.
  3. Step 3: Close the file

    Use fclose() with the file handle.
  4. Final Answer:

    $file = fopen('farewell.txt', 'w'); fwrite($file, 'Goodbye'); fclose($file); -> Option C
  5. Quick Check:

    Open-write-close sequence correct [OK]
Quick Trick: fwrite requires fopen handle, then fclose [OK]
Common Mistakes:
  • Passing filename directly to fwrite()
  • Using 'r' mode for writing
  • Not closing the file after writing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes