PHP - File HandlingWhich of the following is the correct syntax to open a file for reading in PHP?A$fp = fopen('file.txt', 'r');B$fp = fopen('file.txt', 'w');C$fp = fopen('file.txt', 'rw');D$fp = fopen('file.txt', 'a');Check Answer
Step-by-Step SolutionSolution:Step 1: Understand fopen modes'r' opens file for reading only, 'w' for writing (truncates), 'rw' is invalid, 'a' for appending.Step 2: Identify correct mode for readingOnly 'r' mode opens file for reading without modifying it.Final Answer:$fp = fopen('file.txt', 'r'); -> Option AQuick Check:Open file for reading = 'r' mode [OK]Quick Trick: Use 'r' mode in fopen() to open file for reading [OK]Common Mistakes:Using 'w' which truncates fileUsing invalid mode 'rw'Confusing 'a' (append) with read mode
Master "File Handling" in PHP9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PHP Quizzes Classes and Objects - Class declaration syntax - Quiz 2easy Error and Exception Handling - Set_error_handler function - Quiz 4medium File Handling - File existence and info checks - Quiz 7medium Inheritance and Polymorphism - Method overriding - Quiz 11easy Interfaces and Traits - Interface constants - Quiz 10hard Interfaces and Traits - Multiple interface implementation - Quiz 2easy Sessions and Cookies - Setting and reading cookies - Quiz 9hard String Functions - Trim functions - Quiz 13medium Superglobals and Web Context - Input validation and sanitization - Quiz 6medium Superglobals and Web Context - $_REQUEST behavior - Quiz 6medium