← Back to PHP Course | Chapter 6: Strings | Lesson 2 of 8

PHP Escape Characters

Some characters cannot be typed directly inside a string the way you'd expect -- a literal double quote inside a double-quoted string would end the string early, and there is no key on your keyboard for "new line" or "tab" as a literal character. Escape sequences solve this: a backslash followed by a specific letter or symbol tells PHP to insert that special character instead of interpreting the backslash literally.

Escaping Quotes Inside a String

To include a literal double quote inside a double-quoted string, precede it with a backslash: \". Without the backslash, PHP interprets the quote as the end of the string, which almost always breaks the surrounding code with a parse error.

Note: When a string needs many literal double quotes, it is often cleaner to wrap the whole string in single quotes instead, avoiding the need to escape each one.

Warning: A forgotten escape before a quote character is one of the most common causes of a confusing "unexpected token" parse error in PHP.

Example: Escaping Quotes Inside a String

php
<?php
echo "She said \"hello\" to me.";
?>

Newlines and Tabs with \n and \t

\n inserts a line break and \t inserts a horizontal tab, both of which are invisible whitespace characters with no direct keyboard key -- these only work inside double-quoted strings (or heredoc syntax), not single-quoted ones.

Note: Use \n to format multi-line console or log output, and \t to align columns of plain-text output readably.

Warning: \n inside single quotes produces the two literal characters backslash and n, not an actual line break -- a very common source of confusion for beginners.

Example: Newlines and Tabs with \n and \t

php
<?php
echo "Line one\nLine two\tTabbed";
?>

Escaping the Backslash Itself

Because the backslash is the escape character, including a literal backslash in a string requires escaping it too: \\. This matters especially for Windows file paths, regular expression patterns, and any text that itself needs to contain backslashes.

Note: When writing a file path in a double-quoted string, escape every backslash, or switch to single quotes where only \\ still needs escaping but nothing else does.

Warning: A single un-escaped backslash right before a character PHP does recognize (like \n) will be silently consumed as part of that escape sequence instead of appearing as a literal backslash.

Example: Escaping the Backslash Itself

php
<?php
echo "C:\\Users\\Alice";
?>

Escaping the Dollar Sign in Double-Quoted Strings

Double-quoted strings automatically interpolate variables -- a bare $name inside one is replaced with that variable's value. To include a literal dollar sign followed by what looks like a variable name without triggering interpolation, escape it as \$.

Note: If a string needs to show literal PHP-like syntax (for documentation or example output), escape every $ that should not be treated as a variable reference.

Warning: An un-escaped $ followed by letters inside a double-quoted string, where no such variable exists, silently interpolates to an empty string rather than raising an error.

Example: Escaping the Dollar Sign in Double-Quoted Strings

php
<?php
$name = "Alice";
echo "The price is \$name, not $name";
?>

Single vs Double Quotes: Choosing the Right One

Since single-quoted strings only recognize \\ and \', they are simpler and very slightly faster for plain text with no variables or special characters. Double-quoted strings, which support the full escape sequence set plus variable interpolation, are the better choice whenever a string needs newlines, tabs, or embedded variable values.

Note: Default to single quotes for plain static text, and switch to double quotes specifically when you need interpolation or an escape sequence like \n.

Warning: Using double quotes everywhere out of habit, even for strings with no variables or special characters, adds a small unnecessary parsing overhead and can accidentally trigger interpolation on a stray $.

Example: Single vs Double Quotes: Choosing the Right One

php
<?php
echo 'Plain text with no variables';
echo "\n";
echo "Text with a newline\n";
?>
Common Mistakes
  1. Using escape sequences like \n and \t inside single-quoted strings, where PHP only recognizes \\ and \' -- everything else, including \n, is treated as two literal characters.
  2. Forgetting to escape a double quote inside a double-quoted string, which ends the string early and usually causes a parse error.
  3. Over-escaping characters that do not need it inside single-quoted strings, like writing 'It\s when a simple double-quoted "It's" avoids the escape entirely.
Chapter Summary
  • Escape sequences use a backslash followed by a character, like \n (newline), \t (tab), \" (literal quote), and \\ (literal backslash).
  • Double-quoted strings support the full set of escape sequences; single-quoted strings only recognize \\ and \'.
  • Choosing single vs double quotes based on which set of characters you need to include is often simpler than escaping everything.
Browser Support

PHP escape sequences work identically in every PHP version and on every server, since they are a core part of the string-literal syntax itself.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.