PHP Escape Characters
In this page:
"text with \"quotes\" inside"
"line one\nline two" // \n newline
"column\tcolumn" // \t tab
"path\\to\\file" // \\ backslash
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.
Example: Escaping Quotes Inside a String
<?php
// Print "She said \"hello\" to me." to the output
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.
Example: Newlines and Tabs with \n and \t
<?php
// Print "Line one\nLine two\tTabbed" to the output
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.
Example: Escaping the Backslash Itself
<?php
// Print "C:\\Users\\Alice" to the output
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 \$.
Example: Escaping the Dollar Sign in Double-Quoted Strings
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "The price is \$name, not $name" to the output
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.
Example: Single vs Double Quotes: Choosing the Right One
<?php
// Print 'Plain text with no variables' to the output
echo 'Plain text with no variables';
// Print "\n" to the output
echo "\n";
// Print "Text with a newline\n" to the output
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: