Example Description: phpfwrite () function appending and line feed writing

Source: Internet
Author: User
The fwrite () function is used to write strings to a file. the number of written characters is returned successfully. otherwise, FALSE is returned. PHP fwrite () function

The fwrite () function is used to write strings to a file. the number of written characters is returned successfully. otherwise, FALSE is returned.

Syntax:

int fwrite( resource handle, string string [, int length] )

Fwrite () writes the string content to the handle of the file pointer.

Parameter description:


Parameters Description
Handle Required. Specifies the open file to be written. Generally, it is created by the fopen () function.
String Required. Specifies the string to be written to the open file.
Length Optional. Specifies the maximum number of bytes to write.


Ps: if the optional parameter length is specified, the write will stop after the length byte is written or the string is written.

Example

 

Execute this sub-program. in the program directory, the content of the File.txt file is: Hello!

Use the length parameter

In the preceding example, if the length parameter is used, at most length strings are written:

Echo fwrite ($ fh, $ word, 4); // output: 4

PHP fwrite append write

The append write to a file is not related to the fwrite function, but to the mode in which the fopen function opens the file. When fopen opens a file, the mode parameter is set to a, indicating that the file is appended to the file:

 

PHP fwrite line feed

To implement line feed writing in the file, you only need to add line feed n to the place where line breaks are required in the write content:

 

In the above example, n linefeeds are added at the end of the content. To enable n to indicate a line break when writing a file, double quotation marks (for example, in the preceding example) must be used for writing. if single quotation marks are used, n characters are not interpreted as line breaks but are considered as n strings. Click to view details: the difference between single quotation marks and double quotation marks in a PHP string.

N is a line feed in the real sense. in windows, if you want to simulate a line break by carriage return (that is, when you open a file with a WordPad, it is not a black box but a line by line ), you can add the r carriage return before n:

 

PHP fwrite write permission

When opening an existing file (usually in append mode), it is necessary to check whether the file has the write permission to avoid a system error. Use the is_writable function to check whether a file can be written.

The following is an example of a strict check in the append write mode. the code is as follows:

 

The PHP fwrite function only writes strings to a file. the actual result depends on more fopen functions.

[Recommended articles ]:

1. detailed description of php fwrite () function file writing instances

2. what should I do if the line break does not work when the php fwrite () function is written with a line break?

The above is the details of php fwrite () function appending and line feed writing. For more information, see other related articles in the first PHP community!

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.