Home >Backend Development >PHP Tutorial >Why Doesn't '\r\n' Create a Newline in Single-Quoted PHP Strings?

Why Doesn't '\r\n' Create a Newline in Single-Quoted PHP Strings?

Susan Sarandon
Susan SarandonOriginal
2024-12-10 22:56:12682browse

Why Doesn't 'rn' Create a Newline in Single-Quoted PHP Strings?

Creating a Newline Character in PHP

In the pursuit of creating a newline character in PHP, a user encounters a surprising behavior: the sequence 'rn' is rendered as a literal string rather than the expected newline. This issue stems from the use of single-quoted strings.

Single-quoted strings in PHP only recognize two escape sequences: and '. Therefore, the newline escape sequences r and n are not interpreted within single quotes.

Solution:

To create a newline character using a single-quoted string, it must be concatenated with a newline generated elsewhere. This can be achieved using double-quoted strings (e.g., "rn") or the chr function (chr(0x0D).chr(0x0A)).

Alternatively, the newline can be manually typed into the code:

$s = 'some text before the line break
some text after';

Editor Settings:

It is important to check the line break settings of your text editor to ensure that it matches the desired output.

The above is the detailed content of Why Doesn't '\r\n' Create a Newline in Single-Quoted PHP Strings?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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