PHP Escape Sequences: Quotes, Newlines and Tabs

Escape sequences in PHP look simple until a \n stubbornly prints as \n instead of creating a new line. Usually, the backslash is not the problem. The type of string is.

PHP uses a backslash (\) to start an escape sequence. It can let you include quotes and backslashes inside a string, or represent special characters such as newlines and tabs.

The important detail is that PHP interprets escape sequences differently in single-quoted and double-quoted strings.

Quick answer

Most PHP escape sequences work inside double-quoted strings and heredoc strings. For example, \n creates a newline, \t creates a tab, and \" inserts a double quote.

Single-quoted strings are much simpler. PHP normally recognizes only \' for a single quote and \\ for a backslash. A sequence such as \n remains the two literal characters \ and n.

<?php

echo "First line\nSecond line";

echo 'First line\nSecond line';

The first string contains an actual newline character. The second prints \n literally.

How PHP escape sequences work

An escape sequence starts with a backslash followed by another character. PHP gives certain combinations a special meaning while parsing a string.

For example, a double quote normally ends a double-quoted string. Prefixing it with a backslash tells PHP that the quote belongs to the string instead.

<?php

$message = "She said, \"PHP is fine.\"";

echo $message;

The output is:

She said, "PHP is fine."

The same idea applies when you need a literal backslash:

<?php

$path = "C:\\projects\\demo";

echo $path;

This outputs:

C:\projects\demo

Escape sequences can also represent characters that are awkward to place directly in a string, such as a line feed or horizontal tab. We will look at the useful PHP escape sequences next.

Common PHP escape sequences

These are the escape sequences you are most likely to use in normal PHP code:

Escape sequence Meaning
\n Line feed, commonly used for a new line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\e Escape character
\f Form feed
\\ Backslash
\$ Literal dollar sign
\" Double quote

For example:

<?php

echo "Name:\tJohn\nRole:\tDeveloper";

In a terminal or plain-text output, this produces two lines with tab spacing:

Name:   John
Role:   Developer

The exact appearance of tabs depends on where the output is displayed.

Single quotes vs double quotes

This is the part that causes most confusion.

Double-quoted PHP strings interpret escape sequences such as \n, \t, and \". They also perform variable interpolation in PHP strings.

<?php

$name = "John";

echo "Hello $name\nWelcome back.";

Single-quoted strings do not behave the same way. PHP recognizes only two special escape forms there: \\ and \'.

<?php

echo 'It\'s working';
echo '\\home\\user';

Other backslash sequences remain literal:

<?php

echo 'First line\nSecond line';

The output is:

First line\nSecond line

So if \n or \t is unexpectedly appearing in your output, check the quote type first.

Escaping quotes inside PHP strings

You only need to escape the quote character that would otherwise close the current string.

Inside a double-quoted string, escape a double quote:

<?php

$text = "He said, \"Hello.\"";

echo $text;

Inside a single-quoted string, escape a single quote:

<?php

$text = 'It\'s a PHP string';

echo $text;

You can also avoid escaping when the other quote style makes the string clearer:

<?php

echo 'He said, "Hello."';
echo "It's a PHP string";

That is usually easier to read than adding backslashes everywhere.

Escaping the dollar sign

PHP interpolates variables inside double-quoted strings. If you want a dollar sign to appear literally before text that could be read as a variable, escape it with \$.

<?php

$price = 25;

echo "Price: \$$price";

The output is:

Price: $25

You do not normally need this escape in a single-quoted string because variables are not interpolated there.

Octal, hexadecimal, and Unicode escape sequences

PHP can also represent characters by numeric code values inside double-quoted strings. These forms are less common in everyday application code, but they are useful when a character is difficult to type or needs to be expressed explicitly.

Octal escape sequences

An octal escape uses a backslash followed by up to three octal digits:

<?php

echo "\101";

This outputs:

A

Hexadecimal escape sequences

A hexadecimal escape starts with \x followed by one or two hexadecimal digits:

<?php

echo "\x41";

This also outputs A.

Unicode code point escape sequences

PHP also supports Unicode code point escapes using the \u{...} syntax in double-quoted strings.

<?php

echo "\u{2605}";

The output is:

This form is often clearer than manually inserting encoded byte sequences when you know the Unicode code point you need.

Newlines in PHP and HTML output

One practical detail is easy to miss: \n adds a newline character to the output, but HTML does not normally display that character as a visible line break.

<?php

echo "First line\nSecond line";

If you inspect the page source, the newline is there. In normal browser rendering, however, both lines may still appear together because HTML collapses whitespace.

If you need a visible browser line break, output HTML such as <br>:

<?php

echo "First line<br>Second line";

Use \n when you need a real newline in plain text, logs, generated files, command-line output, email source, or similar text formats. The guide to PHP line breaks with \n, PHP_EOL and nl2br() covers these differences in more detail. Use HTML markup when you are controlling visual layout in a web page.

Common mistakes with PHP escape sequences

Using \n inside single quotes

This does not create a newline:

<?php

echo 'First line\nSecond line';

Use double quotes when you want PHP to interpret \n:

<?php

echo "First line\nSecond line";

Forgetting to escape a backslash

Backslashes can start escape sequences inside double-quoted strings, so file paths and similar values sometimes need care.

<?php

$path = "C:\\temp\\file.txt";

echo $path;

For paths that contain no single quotes, a single-quoted string can also be easier to read:

<?php

$path = 'C:\temp\file.txt';

echo $path;

Escaping characters that do not need escaping

Not every quote needs a backslash. If a string is wrapped in single quotes, double quotes can usually appear normally, and vice versa.

<?php

echo 'She said, "Hello."';
echo "It's ready.";

Choosing the suitable quote style often produces cleaner code than adding extra escape characters.

Escape sequences are not output escaping

The word escape is used for several different things in PHP, and they should not be confused.

PHP string escape sequences such as \n, \t, and \" control how characters are represented inside PHP strings. They do not make untrusted data safe for HTML output escaping, SQL, JavaScript, or other output contexts.

For example, escaping a quote with a backslash is not a substitute for HTML escaping with htmlspecialchars():

<?php

$name = '<strong>John</strong>';

echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');

Similarly, do not build SQL queries by manually adding backslashes with addslashes(). Use MySQLi prepared statements for database values.

When to use PHP escape sequences

In normal PHP work, a small set covers most cases: \n for newlines, \t for tabs, \\ for backslashes, and escaped quotes when the quote matches the string delimiter.

The main rule to remember is simple: double-quoted strings interpret many escape sequences, while single-quoted strings recognize only \\ and \'. Once that difference is clear, most PHP escaping problems become much easier to spot.

Photo of Vincy, PHP developer
Written by Vincy Last updated: August 15, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

20 Comments on "PHP Escape Sequences: Quotes, Newlines and Tabs"

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?