Recall & Review
beginner
What does the
fwrite function do in PHP?The
fwrite function writes data to an open file pointer. You must open the file first with fopen, then use fwrite to write content to it.Click to reveal answer
beginner
How does
file_put_contents simplify writing files compared to fwrite?file_put_contents writes a string directly to a file without needing to open or close the file manually. It is a shortcut for simple file writing tasks.Click to reveal answer
beginner
What must you do before using
fwrite to write to a file?You must open the file using
fopen with the correct mode (like 'w' for write). After writing, you should close the file with fclose.Click to reveal answer
intermediate
What happens if you use
file_put_contents on a file that already exists?By default,
file_put_contents will overwrite the existing file content. You can append instead by passing the FILE_APPEND flag.Click to reveal answer
beginner
Write a simple PHP code snippet using
fwrite to write 'Hello' to a file named 'greet.txt'.<?php
$fp = fopen('greet.txt', 'w');
fwrite($fp, 'Hello');
fclose($fp);
?>Click to reveal answer
Which PHP function writes data directly to a file without manually opening it?
✗ Incorrect
file_put_contents writes data directly to a file, handling opening and closing internally.
What mode should you use with
fopen to write and overwrite a file?✗ Incorrect
The 'w' mode opens the file for writing and truncates it to zero length, overwriting existing content.
What does
fclose do in file handling?✗ Incorrect
fclose closes the file pointer opened by fopen, freeing system resources.
How do you append data to a file using
file_put_contents?✗ Incorrect
Passing the FILE_APPEND flag tells file_put_contents to add data to the end of the file instead of overwriting.
Which of these is the correct order to write to a file using
fwrite?✗ Incorrect
You must open the file first, then write, then close it.
Explain how to write text to a file in PHP using
fwrite. Include the steps and functions involved.Think about opening the file, writing, then closing it.
You got /4 concepts.
Describe the difference between
fwrite and file_put_contents when writing files in PHP.One needs more steps, the other is simpler for quick writes.
You got /4 concepts.