Escape sequences and numeric notation in PHP
Hello, Habr. In anticipation of the start of the online course “PHP developer” prepared a traditional translation of the material. We also offer you to watch the recording of the past demo lesson “PHP Ecosystem”.
Many modern programming languages support different ways of using different characters, such as ordinary English Latin letters, numbers, symbols, emoji, and various special characters such as the newline or tab character.
Most characters can be simply typed and used in PHP code as they are. For example, $string = "php.watch"
is a completely valid string in PHP, and $num = 42
is a valid number. It is also possible to use multibyte characters (which require more than one byte to store), for example, this perfectly valid emoji: $emoji = "?"
…
PHP, along with many other programming languages, supports a certain number of escape sequences for various characters that cannot be typed from a regular keyboard, cannot be represented in text form (such as invisible characters or various control characters), or otherwise not are read. These symbols use escape sequencesthat PHP recognizes.
As far as numbers are concerned, PHP supports standard decimal numbers, but it can also use other notations such as binary, octal, hexadecimal and even scientific view… They can make your code more readable and understandable depending on the context.
Double quotes and Heredoc
In PHP, a double quoted string ("string"
) or Heredoc (see below) supports escape sequences and variable interpolation.
PHP will try to interpolate variables if the string literal is inside a double quoted string or Heredoc.
$name="John";
echo "Hi $name"; // "Hi John"
$name="John";
echo <<<HEREDOC
Hi $name
HEREDOC;
// "Hi John"
Alternatively (and preferably) interpolated variables can be enclosed in curly braces so they appear more readable:
$name="John";
echo "Hi {$name}"; // "Hi John"
Single quoted strings ('string'
) and Nowdoc syntax do not interpolate variables:
$name="John";
echo 'Hi $name'; // "Hi $name"
$name="John";
echo <<<'NOWDOC'
Hi $name
NOWDOC;
// "Hi $name"
Only double quoted strings and Heredoc support escape characters…
Escaping characters
Because PHP interprets and interpolates special characters inside double quoted string literals and heredocs, the backslash () is used as an “escape character”.
For example using $name
instead $name
will prevent PHP from interpolating the variable $name
…
$name="John";
echo "Hi $name"; // "Hi $name"
Using the second backslash character prevents the first backslash character from being escaped.
$name="John";
echo "Hi \$name"; // "Hi John"
PHP supports several special escape sequences for special characters. In the above example $
is considered an escape sequence because it overrides PHP interpolation, forcing PHP to literally use the character $
…
Tab characters: t and v
Perhaps the simplest of the control characters is the tab character. The tab character (by pressing the tab key) can be used inside a string literal, but using t
makes it clear instead of a visual pass. Using t
instead of literal tab character also avoids automatic replacement of tab characters with spaces in various IDEs.
echo "FootBar";
Foo Bar
v is a vertical tab… On supported terminals, the vertical tab character goes to the next character on the next line:
echo "FoovBarvBaz";
Foo
Bar
Baz
New lines: r and n
r
(“Carriage return”) and n
(“Line feed”) are newline characters.
Historically, various systems began to use either r
or n
and even Windows uses rn
… For example, Linux and MacOS use the linefeed character (n
) as a newline character, whereas Windows uses the combination rn
(carriage return followed by line feed). Older macOS systems used as newline character r
…
PHP has a constant
PHP_EOL
which always refers to the system newline character.
echo "LeftnLeftnRightnRight";
Left
Left
Right
Right
Escape character: e
Escape characters are often used to send ANSI escape sequences to the terminal. For example, e
followed by [32m
, tells the terminal to change its color to green, and [33m
– to yellow.
echo "e[32mGreen texte[0m e[33mYellow texte[0";
If the above snippet is executed in a terminal that supports ANSI escape sequences, it interprets it and changes the text:
New page character: f
The new page character is an ASCII control character to indicate the end of a page. It allows printers to display the current page and start at the top of the next. When f is passed to a display terminal, it can result in screen clearing, although this is a very rare behavior for most terminal emulation programs.
Octal ASCII character escapes
PHP supports escaping an octal number in its corresponding ASCII character.
For example, the ASCII character P
is equal to 80
in decimal system (see diagram). 80 from decimal number system translated into octal – 120
…
For symbol P
octal escape sequence can be used:
echo "120";
P
Any basic ASCII character can be represented using this notation:
echo "12011012056127141164143150";
PHP.Watch
Any value in the range