Home  >  Article  >  Backend Development  >  The difference between fwrite and file_put_contents in PHP

The difference between fwrite and file_put_contents in PHP

高洛峰
高洛峰Original
2017-06-05 11:28:121779browse

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);

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);
Statement:
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