


Detailed example of php fwrite() function appending and newline writing
PHP fwrite() function
fwrite() function is used to write a string to a file and returns the number of characters written successfully, otherwise it returns FALSE.
Syntax:
int fwrite( resource handle, string string [, int length] )
fwrite() writes the contents of string to the file pointer handle.
Parameter description:
Parameter | Description |
---|---|
handle | Required. Specifies the open file to write to. Generally 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 be written. |
ps: If the optional parameter length is specified, when length bytes are written or after the string is written , writing will stop.
Example
<?php // 要写入的文件名字 $filename = 'file.txt'; // 写入的字符 $word = "你好!"; $fh = fopen($filename, "w"); echo fwrite($fh, $word); // 输出:6 fclose($fh); ?>
Execute this example program. In the same directory as the program, the content of the file.txt file is: Hello!
Use the length parameter
In the above example, if the length parameter is used, at most length strings will be written:
echo fwrite($fh, $word, 4); // 输出:4
PHP fwrite append writing
The append writing to the file actually has nothing to do with the fwrite function, but is related to the mode of opening the file by the fopen function. When fopen opens a file, the mode parameter selects a, which means appending to the file:
<?php $filename = 'file.txt'; $word = "你好!"; $fh = fopen($filename, "a"); echo fwrite($fh, $word); fclose($fh); ?>
PHP fwrite newline writing
If you want to implement it in the file To write with a newline, you only need to add the newline character n where a newline is needed in the written content:
<?php $filename = 'file.txt'; $word = "你好!n"; $fh = fopen($filename, "a"); echo fwrite($fh, $word); fclose($fh); ?>
The above example adds n newline characters at the end of the content. To make n represent a newline when writing a file, you need to use double quotes when writing (as in the example above). If you use single quotes, the n character will not be interpreted as a newline but as an n string. Click to view: PHP string The difference between single quotes and double quotes.
n is already a real line feed. If you want to simulate a carriage return and line feed on the Windows operating system (that is, when you open a file with WordPad, it will not be a black square but listed line by line). You can use n Add r and carriage return in front:
<?php $word = "你好!rn"; ?>
PHP fwrite write permission
When opening an existing file (usually in append writing mode), it is necessary Check whether the file has write permission to avoid a system error. Use the is_writable function to check whether the file is writable.
The following is an example of strict checking in append writing mode. The code is as follows:
<?php $filename = 'file.txt'; $word = "你好!n"; // 确定文件存在并且可写 if (is_writable($filename)) { //打开文件 if (!$fh = fopen($filename, 'a')) { echo "不能打开文件 $filename"; exit; } // 写入内容 if (fwrite($fh, $word) === FALSE) { echo "不能写入到文件 $filename"; exit; } echo "成功地将 $word 写入到文件 $filename"; fclose($fh); } else { echo "文件 $filename 不可写"; } ?>
PHP fwrite function only performs the action of writing strings to the file. The actual result of its behavior depends on More about the fopen function.
【Recommended related articles】:
1.Detailed explanation of the php fwrite() function writing file example
The above is the detailed content of Detailed example of php fwrite() function appending and newline writing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the process of using Excel, we have to perform many different operations. Sometimes we need to wrap a line in a cell. So what exactly do we need to do to wrap a cell? Let’s take a look at how to wrap lines in a computer excel table. [Collection of excel table operation methods] How to wrap rows in excel table? Answer: You can do this by automatically wrapping rows and setting cell formats. 1. Automatic line wrapping 1. We select the area that needs to be wrapped in the table and click [Automatically wrap] on the [Start] page; 2. Then we adjust the width of column A to an appropriate value; 2. Set the cells Format 1. First, we select the area that needs to be wrapped, right-click the mouse, and click [Set Cell Format]; 2. Then in the pop-up

Many users use the BarTender software in their offices. Recently, some new users have asked how to wrap lines in BarTender. Below, the editor will bring you the method of wrapping lines in BarTender. Let us take a look below. 1. In BarTender, click the Create Text button in the toolbar, select Create Single Line Text, and enter the text content. 2. Double-click the created text object to open the text properties dialog box. Switch to the "Text Format" tab and select "Paragraph" for "Type" on the right. 3. Click Close, adjust the size of the text box or enter more text, or wrap the text according to actual requirements.

Wrapping lines in cells in Apple's Excel Apple's Excel software is a powerful spreadsheet tool that provides many convenient functions to help users with data processing and analysis. When using Excel, sometimes we need to enter multiple lines of text in cells to better organize and present the data. However, since Excel for Apple computers is slightly different from the Windows version of Excel, the method of wrapping lines is also different. In the Windows version of Excel, we can directly

Go language is a modern, efficient and concise programming language that is widely used in software development in various fields. In the Go language, outputting text with newlines is very simple and can be achieved by using the Println function provided by the fmt package. Below we will introduce in detail how to output text with line breaks in Go language, as well as related code examples. In the Go language, if you want to output text with newlines, you can use the Println function provided by the fmt package. The Println function will output text in

1. Press the space bar multiple times, move the cursor to the far right, and then enter text to wrap the line. 2. When you need to break a line, click the microphone icon on the keyboard and the voice will say [Line break]. 3. In the memo usage scenario, there is a [Line Break] option in the input method, which can be used to achieve line break by copying. 4. Download other third-party input methods from the app store to use.

PyCharm is a powerful Python integrated development environment, and its automatic line wrapping function can help developers write code more efficiently. This article will introduce how to quickly master the automatic word wrapping function in PyCharm and provide specific code examples. 1. Enable the automatic word wrapping function. In PyCharm, the automatic word wrapping function is not enabled by default and needs to be manually set before it can be used. The specific steps are as follows: Open PyCharm and enter File->Settings;

In PHP development, file operations are very common. Under normal circumstances, we need to perform file reading, writing, deletion and other operations. Among them, the fopen function and fread function can be used to read the file, and the fopen function, fwrite function and fclose function can be used to write the file. This article will introduce how PHP uses fopen, fwrite and fclose to perform file operations. 1. fopen function The fopen function is used to open files. Its syntax is as follows: r

Detailed explanation of how to replace newlines in PHP In PHP development, sometimes we need to replace or process newlines in strings. Line breaks may be expressed differently on different platforms, so they need to be processed uniformly to ensure that strings display consistently in different environments. This article will introduce in detail how to replace newlines in PHP, including common newline characters and specific code examples. 1. Common newline characters In different operating systems, the representation of newline characters may be slightly different. The main newline characters include: Windo
