PHP Basic Tutorial
Escape characters are used to represent characters that are difficult or impossible to type directly in a string, or to insert special characters within a string. These escape characters are typically represented with a backslash (\
) followed by a character or a sequence.
Here’s a list of commonly used escape sequences in PHP:
Escape Sequence |
Description |
Example |
---|---|---|
\' |
Single quote |
'It\'s cool' |
\" |
Double quote |
"She said, \"Hello!\"" |
\\ |
Backslash |
|
\n |
Newline |
"Line 1\nLine 2" |
\r |
Carriage return |
"Hello\rWorld" |
\t |
Tab (horizontal tab) |
"Name\tAge" |
\v |
Vertical tab |
"First\vSecond" |
\b |
Backspace (non-printing) |
"Word\b" |
\f |
Form feed |
"Text\fEnd" |
\xXX |
Hexadecimal character (XX is a two-digit hex number) |
|
\u{XXXX} |
Unicode escape (for characters above 255) |
|
Try it yourself
\
), you need to escape it using \\
.Try it yourself
You can insert newline characters (\n
), carriage return (\r
), and tabs (\t
) inside strings.
Try it yourself
Unicode characters can be represented with \u{XXXX}
, where XXXX
is the Unicode code point (e.g., \u{1F600}
for the smiling face emoji) and hexadecimal character can be represented with \xXX
, where XX
is the Hexadecimal code point .
Try it yourself