PHP - File HandlingWhich 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);Check Answer
Step-by-Step SolutionSolution:Step 1: Open file in write modeUse fopen() with 'w' to open for writing.Step 2: Write string to fileUse fwrite() with the file handle and string.Step 3: Close the fileUse fclose() with the file handle.Final Answer:$file = fopen('farewell.txt', 'w'); fwrite($file, 'Goodbye'); fclose($file); -> Option CQuick 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 writingNot closing the file after writing
Master "File Handling" in PHP9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PHP Quizzes Array Functions - Array filter function - Quiz 6medium Classes and Objects - Object instantiation with new - Quiz 13medium Classes and Objects - Constructor method - Quiz 8hard Error and Exception Handling - Throwing exceptions - Quiz 12easy File Handling - Reading files (fread, fgets, file) - Quiz 1easy Inheritance and Polymorphism - Why inheritance is needed - Quiz 5medium Inheritance and Polymorphism - Parent keyword behavior - Quiz 3easy Inheritance and Polymorphism - Why inheritance is needed - Quiz 3easy Sessions and Cookies - How sessions work in PHP - Quiz 1easy Superglobals and Web Context - Form handling execution flow - Quiz 1easy