Home 类库下载 PHP类库 Why the newline character does not work when writing newline in PHP fwrite

Why the newline character does not work when writing newline in PHP fwrite

Oct 20, 2016 pm 02:45 PM
php basics

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 = &#39;file.txt&#39;;
 $word = &#39;你好!\r\n欢迎来到www.webkaka.com&#39;;
 $fh = fopen($filename, "a"); //w从开头写入 a追加写入
 echo fwrite($fh, $word);
 fclose($fh);
?>
Copy after login

  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 = &#39;file.txt&#39;;
$word = "你好!\r\n欢迎来到www.webkaka.com";
$fh = fopen($filename, "a"); //w从开头写入 a追加写入
echo fwrite($fh, $word);
fclose($fh);
Copy after login

 

 

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


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What front-end developers need to know about PHP What front-end developers need to know about PHP Mar 29, 2024 pm 03:09 PM

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

A Beginner's Guide to PHP A Beginner's Guide to PHP May 25, 2023 am 08:03 AM

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

Weak foundation in PHP written test? Try these study methods! Weak foundation in PHP written test? Try these study methods! Mar 01, 2024 pm 01:39 PM

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

Essential for beginners: PHP basic errors and solutions Essential for beginners: PHP basic errors and solutions May 11, 2023 am 08:28 AM

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: &lt;?phpecho"He

PHP Values ​​Overview: Understand the important concepts of PHP PHP Values ​​Overview: Understand the important concepts of PHP Mar 22, 2024 pm 03:09 PM

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

PHP basic tutorial array function PHP basic tutorial array function Jun 20, 2023 pm 01:39 PM

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

10 Tips for Reading PHP Documentation 10 Tips for Reading PHP Documentation May 24, 2023 pm 09:21 PM

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

Necessary for building a dreamweaver website: Master several key knowledge points of PHP Necessary for building a dreamweaver website: Master several key knowledge points of PHP Mar 27, 2024 pm 03:09 PM

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

See all articles