You have a PHP script and you need to open a file, write to it, and then close it. How would you do this?
- Use the fopen() function to open the file and obtain a file handle, use the fwrite() function to write content to the file, and then use the fclose() function to close the file.
- Use the file_put_contents() function to directly write content to the file without the need for explicit file handle and closing operations.
- Use the readfile() function to read the file and write its contents to the output buffer.
- Use the include() function instead of the fopen() function.
To open a file, write to it, and then close it in PHP, you would use the fopen() function to open 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. Proper file handling includes opening, writing, and closing the file in a structured manner.
Loading...
Related Quiz
- What PHP superglobal array holds the session variables?
- What are some differences between the include and require statements in PHP?
- PHP uses the global keyword to make a local variable have global scope.
- Which of the following is used in PHP to declare a floating-point number?
- You are writing a PHP script and you need to use a callback function. How would you do this?