


What should I do if the newline character does not work when writing with php fwrite() function?
Many novices will encounter one of the most common problems that must be solved when using the php fwrite() function, which is line break writing. We all know the line break character of PHP: \n, and the carriage return character: \r. When a line break is needed, the combination "\r\n" is usually used. But
is why the \n newline character does not work when we use fwrite to write a file. This article will take you to understand what to do when the newline character does not work when the php fwrite() function writes a newline
Let’s first look at the following example:
<?php $filename = 'file.txt'; $word = '你好!\r\n欢迎来到www.php.cn'; $fh = fopen($filename, "a"); //w从开头写入 a追加写入 echo fwrite($fh, $word); fclose($fh); ?>
In the above example, we have used the carriage return and line feed character, but found that when the file.txt file was opened, the carriage return and line feed effect did not appear.
Why is this? Because the carriage return and line feed character "\r\n" is not parsed as a line feed character, but is directly output as a character.
Why does this happen and how to solve it?
In fact, it’s all caused by single and double quotation marks! We can replace the single quotes "'" in the $word definition string with double quotes """. The correct way to write it is as follows:
<?php $filename = 'file.txt'; $word = "你好!\r\n欢迎来到www.php.cn"; $fh = fopen($filename, "a"); //w从开头写入 a追加写入 echo fwrite($fh, $word); fclose($fh); ?>
Now let's open the file.txt file and take a look:
I found that there is no problem with carriage return and line feed.
[Recommended related articles]:
Detailed example of php fwrite() function appending and newline writing
The above is the detailed content of What should I do if the newline character does not work when writing with php fwrite() function?. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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


![Spellcheck not working in Teams [Fixed]](https://img.php.cn/upload/article/000/887/227/170968741326618.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
We've started noticing that sometimes spellcheck stops working for Teams. Spell check is an essential tool for effective communication, and any attack on it can cause considerable disruption to workflow. In this article, we'll explore common reasons why spell check might not be working as expected, and how to restore it to its previous state. So, if spell check is not working in Teams, follow the solutions mentioned in this article. Why doesn't Microsoft spell check work? There may be several reasons why Microsoft spell check is not working properly. These reasons include incompatible language settings, disabled spell check function, damaged MSTeam or MSOffice installation, etc. Also, outdated MSTeams and MSOf

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.

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

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

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;

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

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
