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.
good explanation
Thank you Sruthy.
Hello Vincy, how do you escape php characters inside of php? I know that sounds crazy, but that’s the problem I’m having right now. Ex. like the below. Thanks.
<?php"
echo "<link href='css/main.css'”;
?>
Need to escape the above href line.
You don’t need to escape the `href` itself. The issue is the quotes. Use matching straight quotes, for example:
“`php
“;
?>
“`
Also make sure you are not using curly quotes like `”`, as PHP will not treat them as normal string quotes.
Its working, thanks
Welcome Pavani
Very uesful, especially the list of commonly used ones.
Thank you Nate.
Thanks for the knowledge,
It really help a lot,
It’s well explanatory.
Welcome Adeyemi.
Thanks !!
Welcome Nikunj.
Good,i found this amazing website.
Thanks Nikunj
Thanks for the info. Much appreciated!!
Welcome Mohammed
Nice explanation.
It is very helpful.
Thanks Jasmin
Thanks friend!
Welcome my dear friend.