You need to create a file in your PHP script, write to it, and ensure that the file is closed properly after writing. How would you do this?

  • Use the fopen() function with 'w' mode to create the file and obtain a file handle, then use the fwrite() function to write content to the file. Finally, use the fclose() function to close the file.
  • Use the file_put_contents() function to create the file and write content to it. The function handles file creation and writing, so no explicit file handle or closing is required.
  • Use the touch() function to create the file, then use the fwrite() function to write content to the file. Finally, use the fclose() function to close the file.
  • Use the mkdir() function to create a directory instead of a file.
To create a file, write to it, and ensure proper closing in PHP, you would use the fopen() function with 'w' mode to create the file and obtain a file handle. Then, you can use the fwrite() function with the file handle to write content to the file. Finally, you would use the fclose() function to close the file and release the associated resources. This ensures that the file is created, written to, and closed properly.
Add your answer
Loading...

Leave a comment

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