You are working on a PHP script and need to open a file, read its contents, and then close it. What steps would you take?

  • Open the file using fopen(), read its contents using fread() or other file reading functions, and close the file using fclose()
  • Use readfile() to directly output the file contents, and then close the file using fclose()
  • Use file_get_contents() to read the entire file into a string, and then close the file using fclose()
  • Open the file using fopen(), use file() to read the file line by line into an array, and then close the file using fclose()
To open a file, you would use fopen() with the appropriate file path and mode. Then, you can use fread() or other file reading functions to read the contents of the file. Finally, you would close the file using fclose() to release the resources associated with the file and free up memory. This ensures proper cleanup and prevents resource leaks.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *