The difference between file_put_contents and fwrite in reading and writing files

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.