PHP - Escape Characters
In PHP, escape characters are used to insert special characters into strings—especially within double-quoted (" "
) or heredoc strings. They’re introduced with a backslash (\
).
Common Escape Sequences in PHP
Escape Sequence | Meaning |
---|---|
\n |
New line (line feed) |
\r |
Carriage return |
\t |
Horizontal tab |
\\ |
Backslash |
\" |
Double quote |
\$ |
Dollar sign |
\v |
Vertical tab (less common) |
\f |
Form feed (less common) |
Example
Important Notes
-
Single-quoted strings (
' '
) do not parse most escape sequences—only\\
and\'
are recognized: -
Use heredoc (
<<<
) or nowdoc (<<<'
) for multi-line strings, with similar escape rules:-
Heredoc behaves like double-quoted strings (parses escape characters).
-
Nowdoc behaves like single-quoted strings (no escape character parsing except
\\
and\'
).
-