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

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
