PHP Escape Characters
In this page:
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
echo "She said \"hello\" to me.";
?>
Login to try C/C++/Java/PHP code in the editor
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
echo "Line one\nLine two\tTabbed";
?>
Login to try C/C++/Java/PHP code in the editor
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
echo "C:\\Users\\Alice";
?>
Login to try C/C++/Java/PHP code in the editor
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
$name = "Alice";
echo "The price is \$name, not $name";
?>
Login to try C/C++/Java/PHP code in the editor
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
echo 'Plain text with no variables';
echo "\n";
echo "Text with a newline\n";
?>
Login to try C/C++/Java/PHP code in the editor
- 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.
- Forgetting to escape a double quote inside a double-quoted string, which ends the string early and usually causes a parse error.
- 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.
- 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.
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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: