Show
Many modern programming languages support various way to use various characters such as simple English Latin characters, numbers, symbols, Emojis, and various special characters such as a new line or a tab character. Most of the characters can be simply typed from a keyboard, and used in a PHP snippet as-is. For
example, PHP, among many other programming languages, support several character escaping sequences to use various characters that cannot be typed from a standard keyboard, cannot be represented in a text form (such as invisible characters, or various control characters), or otherwise not readable. These characters use character escaping sequences that PHP recognizes. For numbers, PHP supports the standard decimal numbers, but it is also possible to use other notations such as binary, octal, hexadecimal, and even scientific notation. They can make the code more readable and clearer depending in various contexts. Double Quotes and HeredocIn PHP, a
string with double quotes ( Variable interpolation means that PHP will try to interpolate variables if the string literal is inside a double-quoted string, or a Heredoc.
Alternately, and preferably, variables being interpolated can be more readable curly braces:
Single-quoted strings (
Character EscapingBecause PHP interprets and interpolates special characters inside double-quoted string literals and heredoc string literals, the backslash sign ( For
example, using
Using two backslash characters prevent the backslash character itself from being the escaping character.
PHP supports several special escape sequences for special characters. In the example above Tab Characters: \t and \vPerhaps the simplest one is the tab character. It is possible to use a tab character inside a string literal, but using the
New Lines: \r and \n
Historically, various systems started to use either
Escape character: \eThe escape character is used to often used to send ANSI escape sequences to a terminal. For example,
If the snippet above is run in a terminal that supports ANSI escape sequences, it will interpret it and modify the text: Form-Feed character: \fForm-feed character is an ASCII control character for page breaks.
Printers might eject the current page and start from the top of another. When Octal ASCII Character Escaping SequencesPHP supports escaping an Octal number to its ASCII character. For example, the ASCII character (see chart) for An Octal character escape sequence can be used for the
It is in fact possible to represent any basic ASCII character with this notation:
Any value in the range of Note that
extended ASCII characters (128 through 255) numbers are not compatible with UTF-8. PHP considers a value of While PHP accepts such values, they are considered invalid characters in a UTF-8 context. Hexadecimal ASCII Character Escaping SequencesSimilar to the
Octal character escaping sequences, PHP also allows Hexadecimal numbers in a character escaping sequence with the It only allows one byte, which means the valid range is Further, Hexadecimal characters are not case-sensitive (i.e. ASCII
The same
Unicode Character Escaping SequencesPHP supports using any Unicode character with the
PHP will throw a Parser error
if the Unicode character is beyond the
The
The
Binary StringsSeveral years ago, PHP 5.2.1 introduced a new string syntax called "binary strings". It was merely a syntax, and it was meant as a forward-compatibility improvement for the upcoming PHP 6. The syntax was to prefix the quotes single/double quoted string with
Binary string syntax has no special functionality, and serves no purpose other than being a historical remnant, a hair-pulling interview question, a tidbit for PHP articles, and confusing fellow PHP developers. This syntax is not deprecated, nor planned to be removed in a future PHP version. Numeric NotationsWhen using numeric literals in a PHP script, PHP expects decimal values by default. However, PHP also allows other numeric notations such as binary numbers, octal numbers, hexadecimal numbers, and scientific notation. From PHP 7.4, PHP also allows underscore numeric separators for better readability for long numbers. Binary Numeric NotationAny numeric literal that starts with prefix
With underscore numeric separators, it is possible to use underscores for better readability.
Octal Numeric NotationPHP accepts octal numeric values with the
Hexadecimal Numeric NotationHexadecimal numbers are interpreted with
Scientific Numeric NotationPHP also supports scientific "E notation" for float values.
The
E notation is equivalent to The readability advantage of scientific numeric notation is more pronounced for numbers that are very precise, or large numbers.
PHP supports various forms of character escaping sequences and numeric notations. It recently added
explicit Octal numeric notation with Note that these character escape sequences and numeric notations are not interpreted in user-input. For example, casting an underscore-separated number to an integer will not make use of PHP's underscore-numeric-seprator feature for user-provided strings.
Further chraracter escaping sequences used in user-input are not evaluated either. For example, if a form submits What is meant by escaping characters?In computing and telecommunication, an escape character is a character that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of metacharacters.
What is special character in PHP?Special characters in HTML are represented by entities such as & and < . There are two PHP functions that turn special characters in a string into their entities: one for removing HTML tags, and one for extracting only meta tags.
Why do we escape special characters?The following table shows some examples of special characters that do not require escaping.
...
Escaping special characters.. What are escape characters give example?It is composed of two or more characters starting with backslash \. For example: \n represents new line.
...
List of Escape Sequences in C.. |