Why the newline character does not work when writing newline in PHP fwrite
When we use fwrite to write files, novices will encounter one of the most common problems that must be solved, and that is newline writing.
We all know the line break character of PHP: n and carriage return character: r. When a line break is needed, the combination "rn" is usually used. But why does n newline character not work when we use fwrite to write a file? Let’s look at the following example first:
<?php $filename = 'file.txt'; $word = '你好!\r\n欢迎来到www.webkaka.com'; $fh = fopen($filename, "a"); //w从开头写入 a追加写入 echo fwrite($fh, $word); fclose($fh); ?>
The carriage return and line feed character "rn" is added to the string of $word, but the output result is not as expected. The carriage return and line feed character "rn" is not included. is parsed as a newline character, but is directly output as a character.
Why does this happen? After research, it turns out that the single and double quotes are causing the trouble! We just replace the single quotes "'" in the $word definition string with double quotes """. The correct way to write it is as follows :
$filename = 'file.txt'; $word = "你好!\r\n欢迎来到www.webkaka.com"; $fh = fopen($filename, "a"); //w从开头写入 a追加写入 echo fwrite($fh, $word); fclose($fh);
In the above example, echo fwrite() displays a number, which represents the length of the string.
Knowledge expansion
Use double quotes (") to define the string , PHP understands more escape sequences for special characters:
Transfer sequence description
nLine feed
rCarriage return
tHorizontal tab character
[/td>Backslash
$Dollar sign
" Double quotes
[0-7]{1,3} This regular expression sequence matches a character represented in octal notation
x[0-9A-Fa-f]{1,2} This regular expression sequence matches A character represented in hexadecimal notation

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



As a front-end developer, understanding PHP is very necessary. Although PHP is a back-end development language, mastering a certain amount of PHP knowledge can help front-end developers better understand the entire web development process, improve work efficiency, and collaborate better with back-end developers. In this article, we will discuss some PHP-related knowledge that front-end developers need to know and provide specific code examples. What is PHP? PHP (HypertextPreprocessor) is a server

PHP is a popular front-end programming language. It is powerful, easy to learn and use, and is widely used in website development and maintenance. For beginners, getting started with PHP requires a certain amount of learning and mastering. Here are some guides for beginners in PHP. 1. Learn basic concepts Before learning PHP, you need to understand some basic concepts. PHP is a scripting language that issues instructions to web servers. Simply put, you can use PHP to generate HTML code and send it to the browser to eventually render on the web page

PHP, as a scripting language widely used in web development, has become one of the necessary skills for many Internet companies to recruit technical talents. However, for some learners who are just getting started or have a weak foundation, learning PHP may encounter some difficulties. How can you better improve your basic skills when facing the PHP written test? Next, we will introduce some learning methods, hoping to help everyone improve the basic knowledge and skills of PHP. 1. Develop the habit of reading PHP official documentation. PHP official documentation is a learning

In the process of learning PHP, beginners often encounter various errors. Although this is a natural process of learning, many beginners often lose patience because of improper handling of mistakes. This article will introduce basic PHP errors and solutions, aiming to help beginners get started with PHP more easily. 1. Syntax error 1.1 Missing semicolon In PHP, statements must end with a semicolon. If you accidentally omit a semicolon, an error will be reported. For example, the following code results in an error: <?phpecho"He

Overview of PHP values: To understand the important concepts of PHP, specific code examples are required. PHP (Hypertext Preprocessor) is a scripting language widely used in web development. It can be embedded in HTML or executed as a standalone script. In web development, it is important to understand some important concepts of PHP to write efficient and maintainable code. In this article, we will introduce some important concepts of PHP and provide specific code examples to help readers understand better. variable changes

Array function is one of the most commonly used functions in PHP, which can be used to create, operate and manage arrays. When developing applications, using array functions can greatly improve development efficiency. This article will introduce some basic usage and examples of array functions in PHP to help everyone better understand and master array functions. 1. Creation and initialization of arrays Arrays in PHP can be created in the following ways: //Create arrays through the array() function $arr=array("a",&q

PHP is a very popular open source server-side scripting language that is widely used in web development. To become a good PHP programmer, reading official documentation is essential. Whether you are a beginner or an experienced developer, these tips will help you read PHP documentation more effectively. Understand the document structure PHP official documentation is divided into multiple parts, including manuals, reference manuals, FAQs, extension library documents, etc. Before you start reading, understand the structure of the document and find the parts you need. Using the search function PHP documentation

In today's Internet era of information explosion, websites have become an important way for display and promotion in all walks of life, and PHP, as the most popular server-side scripting language, is undoubtedly one of the essential skills for many website developers. To become proficient in PHP, you first need to master several key knowledge points and deepen your understanding through specific code examples. 1. PHP basic syntax The basic syntax of PHP is similar to most programming languages, including variables, data types, operators, conditional statements, loop statements, etc. Here is a simple example
