File_put_contents and fwrite have the following differences: $ james = fopen ("index.htm", "a"); fwrite ($ james, $ rose); fclose ($ james ); type 2: file_put_content file_put_contents and fwrite
First:
$james=fopen("index.htm","a");
fwrite($james,$rose);
fclose($james);
Second:
file_put_contents('index.htm',$rose);
What is the difference between the two methods for reading and writing files? The second type of code is enough, so the second type is often used. Under what circumstances will the first and the second be used? What are the advantages and disadvantages? Share:
------ Solution --------------------
The second approach is implemented through the first approach. The first feature is more powerful. The key is the second fopen parameter.
------ Solution --------------------
File_put_contents is a packaging of file operation functions.
Used to simplify file write operations
It should be noted that your first method is not equivalent to the second method.
$ James = fopen ("index.htm", "");
A indicates that the written content is appended to the original content
Which is equivalent
File_put_contents('index.htm', $ rose, FILE_APPEND );
Equivalent to file_put_contents('index.htm', $ rose ).
$ James = fopen ("index.htm", "w ");
Fwrite ($ james, $ rose );
Fclose ($ james );
The difference between packaging and non-packaging is:
Low flexibility and simplicity after packaging
Strong flexibility without packaging, but more complicated
------ Solution --------------------
As mentioned above, the difference is that when there are special requirements for opening and writing a file, such as appending a string after the file... You must use the fopen knife... to open the write, you can directly use file_put_contents. file_put_contents is a function that simplifies multiple operations.