Print newline in PHP in single quotes

Print newline in PHP in single quotes

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Myth of Literal Newlines in Single Quotes

There is a common misconception among PHP developers that single quotes behave differently from double quotes regarding escape sequences. Many programmers assume that because single quotes do not parse variable interpolation like {$var}, they also fail to interpret \n as a newline character. If you try to print a string with a literal backslash and the letter 'n' inside single quotes, you will indeed see two characters in your output. However, this behavior is consistent with how PHP handles all strings, regardless of the delimiter chosen.

In PHP, the distinction between single-quoted ('...') and double-quoted ("...") strings primarily lies in variable expansion and escape sequence processing. Double quotes allow you to interpolate variables (e.g., "Hello $name") and interpret certain escape sequences like \n, \t, and \\. Single quotes, by design, treat everything inside them as literal text. This means that within single quotes, the sequence \n is strictly interpreted as a backslash followed by an 'n', not a newline.

If your goal is to output a newline character using single quotes, you cannot use the escape sequence \n. Instead, you must rely on ASCII control characters or specific byte values. The newline character in the ASCII table corresponds to decimal 10 (or hexadecimal 0x0A). You can output this directly by concatenating the integer value with the string:

<?php
echo 'Text before' . "\n" . 'Text after'; // Works, because \n is inside double quotes here or escaped in single quotes context if used differently
echo 'Text before' . chr(10) . 'Text after'; // Correct approach for single quotes

Wait, let's correct that mental model. If you are strictly inside a single-quoted string literal, you cannot use \n to represent a newline because the parser sees it as two characters. To achieve a newline purely within the context of a single-quoted string definition without relying on variable interpolation, you must actually include the control character itself or use chr().

The most robust way to handle this when using single quotes is to use the chr() function. This function takes an integer representing the ASCII code and returns the corresponding character. Since a newline is ASCII 10, chr(10) produces the exact byte needed.

<?php
$message = 'Line one' . chr(10) . 'Line two';
echo $message;

Alternatively, you can use the octal representation of the newline character. In PHP, \012 represents the ASCII value 10. Interestingly, while single quotes do not interpret standard C-style escapes like \n, they do interpret octal and hexadecimal escape sequences if they are part of the string literal syntax in some contexts, but strictly speaking, inside a '...' block, \n is literal. However, chr(10) is the semantic choice that remains unambiguous.

Here is why developers often prefer single quotes for strings containing backslashes:

<?php
// Double quotes require escaping the backslash to prevent confusion with \n
$path = "C:\Users\Name"; 

// Single quotes treat backslashes literally, no escaping needed
$path = 'C:\Users\Name'; 

When constructing paths or strings containing many backslashes, single quotes save you from writing repetitive \\. If you are inside single quotes and need a newline, you simply cannot type \n and expect it to work. You must either press the Enter key (literal line break in the source code), use chr(10), or switch to double quotes if variable interpolation is also required.

It is worth noting that relying on literal newlines in source code (pressing Enter inside the string) is generally a bad practice for indentation and readability, which brings us back to the original question. If you want to maintain clean code formatting while using single quotes, chr(10) is the standard solution. It keeps your code compact and ensures that the output matches your intent regardless of how many times the file is reformatted or indented.

For those working within the Laravel ecosystem, this distinction remains relevant even when dealing with blade templates or configuration arrays. While you might find helper functions in the Laravel Company resources that abstract string handling, understanding the underlying PHP string mechanics ensures that custom logic behaves predictably across different environments. When defining strings in your application logic where you cannot use double quotes due to variable name conflicts, remembering that \n is literal inside single quotes prevents subtle bugs where newlines disappear from your output.

Our network