Home >Backend Development >PHP Tutorial >Why Doesn't '\r\n' 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!