PHP - File HandlingHow can you append text to a file safely in PHP while ensuring the file is closed properly?Afile_put_contents('file.txt', 'More text');B$file = fopen('file.txt', 'a'); fwrite($file, 'More text'); fclose($file);Cfwrite('file.txt', 'More text'); fclose('file.txt');D$file = fopen('file.txt', 'w'); fwrite($file, 'More text'); fclose($file);Check Answer
Step-by-Step SolutionSolution:Step 1: Understand file open modes'a' mode opens file for appending, preserving existing content.Step 2: Check fwrite and fclose usagefwrite writes to opened file handle, fclose closes it properly.Final Answer:$file = fopen('file.txt', 'a'); fwrite($file, 'More text'); fclose($file); -> Option BQuick Check:Use fopen with 'a' and fclose for safe append [OK]Quick Trick: Use fopen('file', 'a') to append safely [OK]Common Mistakes:Using 'w' mode overwrites filePassing filename to fwriteNot closing 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