Three definitions of PHP strings

Source: Internet
Author: User
Three definitions of PHP strings: single quotes, double quotes, delimiters 1, and single quotes: the simplest way to specify a simple string is to enclose it with single quotes (characters. In a string enclosed by single quotes, to represent a single quotation mark, you must use a backslash () to escape it. it is the same as many other languages. If a backslash is required before single quotes or at the end of a string, two backslashes are required. Three definitions of the PHP string enclosed in single quotes
PHP strings can be defined in three ways: single quotes, double quotation marks, and delimiters.
1. single quotes: the simplest way to specify a simple string is to enclose it with single quotes (character. In a string enclosed by single quotes, to represent a single quotation mark, you must use a backslash (\) to escape it. it is the same as many other languages. If a backslash is required before single quotes or at the end of a string, two backslashes are required. All other special characters in a string enclosed by single quotes will be fully expressed. that is to say, the variables and escape sequences in single quotes strings will not be replaced by variable values.

2. double quotation marks: If double quotation marks (") are used to enclose strings, PHP can parse escape sequences of more special characters: \ n (line feed LF or ASCII character 0x0A (10 )), \ r (press enter (CR or ASCII character 0x0D (13), \ t (horizontal tab HT or ASCII character 0x09 (9), \ (backslash), \ $ (dollar sign), \ "(double quotation marks), \ [0-7] {} (this regular expression sequence matches a character represented by the octal symbol), \ x [0-9A-Fa-f] {1, 2} (this regular expression sequence matches a character represented by a hexadecimal symbol). In addition to these escape characters, if you try to escape any other character, the backslash itself will also be displayed! Before PHP 5.1.1, the backslash in \ {$ var} is not displayed.
When a string is specified using double quotation marks or delimiters, the variables in the string are parsed. There are two types of syntax: one is simple and the other is complex.
Simple syntax is the most common and convenient. It provides methods for parsing variables, array values, or object attributes (if you encounter dollar signs ($ ), the parser retrieves as many characters as possible to form a valid variable name. To explicitly specify the end of a name, enclose the variable name in curly brackets. You can also parse array indexes or object attributes. For an array index, the right square brackets (]) indicate the end of the index. Object attributes apply the same rules as simple variables, although they are not as tricky as variables ).
Complex syntax is introduced in PHP 4 and can enclose an expression in curly brackets (in fact, this syntax can contain any value in the namespace in the string. Write an expression in the same way as the string, and use {and} to include it. Because "{" cannot be escaped, this syntax is recognized only when $ is followed by {(use "{$" to get a literal "{$ "). For example: $ {test}, {$ test ['t'] ['E'] ['s'] ['t']}, {$ test-> test }).

3. delimiters: Another method used to define the string using the delimiters syntax ("<"), which is included in PHP 4 .. An identifier should be provided after <, followed by a string, followed by the end string of the same identifier. The end identifier must start from the first column of the row. Similarly, the identifier must follow the naming rules for any other tag in PHP: it can only contain letters, numbers, underscores (_), and must start with an underscore or a non-digit character. It is important to note that the row of the end identifier cannot contain any other character, except a semicolon. This especially means that the identifier cannot be indented, and there cannot be any spaces or tabs before or after the semicolon. It is also important to realize that the first character before the end identifier must be a line break defined in your operating system. For example, \ r is used in a Macintosh system. If this rule is broken so that the end identifier is not "clean", it will not be regarded as the end identifier, and PHP will continue searching for it. If an end identifier cannot be found in this case, a syntax error occurs in the last line of the script. Class members cannot be initialized using the delimiter syntax. Use other string syntax instead.
The delimiter text is the same as the double quotation mark string, but it does not contain double quotation marks. This means that no escape quotation marks are required in the delimiter text, but the escape code listed above can still be used. The variable is expanded, but when the complex variable is expressed in the delimiter text, you should also note the same as the string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.