0
0
PHPprogramming~10 mins

Writing files (fwrite, file_put_contents) in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writing files (fwrite, file_put_contents)
Open file with fopen or use file_put_contents
Write data to file
Check if write succeeded
Close file if fopen used
End
This flow shows how PHP writes data to a file using fopen/fwrite or file_put_contents, then closes the file if needed.
Execution Sample
PHP
<?php
$file = fopen('test.txt', 'w');
fwrite($file, "Hello\n");
fclose($file);
?>
This code opens a file for writing, writes 'Hello' with a newline, then closes the file.
Execution Table
StepActionFunction CallResultFile State
1Open file for writingfopen('test.txt', 'w')File handle resourceFile opened, empty content
2Write string to filefwrite(file handle, "Hello\n")6 bytes writtenFile contains 'Hello\n'
3Close filefclose(file handle)trueFile closed, content saved
4End--File closed, content saved
💡 File closed after writing, data saved to 'test.txt'
Variable Tracker
VariableStartAfter fopenAfter fwriteAfter fclose
$filenullresource (file handle)resource (file handle)null
Key Moments - 3 Insights
Why do we need to close the file after writing?
Closing the file with fclose ensures all data is saved and resources are freed, as shown in step 3 of the execution_table.
What happens if fopen fails to open the file?
If fopen fails, it returns false instead of a resource. You should check this before calling fwrite to avoid errors.
How is file_put_contents different from fopen/fwrite?
file_put_contents combines opening, writing, and closing in one call, simplifying the process as it handles all steps internally.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the file state after step 2?
AFile is closed and empty
BFile is open and contains 'Hello\n'
CFile is open but empty
DFile is closed and contains 'Hello\n'
💡 Hint
Check the 'File State' column in row for step 2 in execution_table
At which step is the file handle resource no longer available?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at variable_tracker for $file after fclose in step 4
If we replace fopen/fwrite/fclose with file_put_contents, how many steps in execution_table would be combined?
A3 steps
B2 steps
C1 step
D4 steps
💡 Hint
file_put_contents does opening, writing, and closing in one call, combining steps 1, 2, and 3
Concept Snapshot
PHP file writing:
- fopen('file', 'w') opens file for writing
- fwrite(handle, data) writes data
- fclose(handle) closes file
- file_put_contents('file', data) does all in one
Always close files to save data and free resources.
Full Transcript
This lesson shows how PHP writes data to files using fopen, fwrite, and fclose functions, or the simpler file_put_contents function. First, fopen opens the file and returns a handle. Then fwrite writes the string data to the file. Finally, fclose closes the file to save changes. Alternatively, file_put_contents does all these steps in one call. Closing the file is important to ensure data is saved and resources are freed. The execution table traces each step and the file's state. The variable tracker shows how the file handle changes from null to resource and back to null after closing. Key moments clarify why closing is needed, what happens if fopen fails, and how file_put_contents simplifies writing. The quiz tests understanding of file state after writing, when the handle is closed, and how steps combine with file_put_contents.