0
0
PHPprogramming~5 mins

Writing files (fwrite, file_put_contents) in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afclose
Bfopen
Cfwrite
Dfile_put_contents
What mode should you use with fopen to write and overwrite a file?
A'r'
B'a'
C'w'
D'x'
What does fclose do in file handling?
ACloses an open file pointer
BWrites data to a file
CReads data from a file
DDeletes a file
How do you append data to a file using file_put_contents?
AUse the FILE_APPEND flag
BUse the 'w' mode
CUse the 'r' mode
DUse the 'x' mode
Which of these is the correct order to write to a file using fwrite?
Afwrite, fopen, fclose
Bfopen, fwrite, fclose
Cfclose, fwrite, fopen
Dfopen, fclose, fwrite
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.