The difference between fwrite and file_put_contents in PHP

高洛峰
Release: 2023-03-01 08:54:01
Original
1834 people have browsed it

Similar points: The file_put_contents() function writes a string to a file, which has the same function as calling fopen(), fwrite() and fclose() in sequence.

Difference: Using FILE_APPEND in the file_put_contents() function can avoid deleting the existing content in the file, that is, realizing the append function when writing the same file multiple times.

For example:

echo file_put_contents("test.txt","Hello World. Testing!",FILE_APPEND);
Copy after login

file_put_contents writes the string into test.txt in the form of append,

fwrtie will clear the previous record and only retain the currently written content

$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!