PHP basic string (1 ). String is a series of characters. In PHP, the character is the same as the byte, that is, there are a total of 256 different characters. This also implies that PHP does not support Unicode locally.
String
String is a series of characters. In PHP, the character is the same as the byte, that is, there are a total of 256 different characters. This also implies that PHP does not support Unicode locally. For more information about Unicode support, see utf8_encode () and utf8_decode.
Note: PHP does not impose an implementation range on the size of a string, so there is no reason to worry about long strings.
Syntax
Strings can be defined in three literal methods.
Single quotes
Double quotation marks
Delimiters
Single quotes
The simplest way to specify a simple string is to enclose it with single quotes (character.
To express a single quotation mark, you need to escape it with a backslash (), which is the same as that in many other languages. If a backslash is required before single quotes or at the end of a string, two backslashes are required. Note that if you try to escape any other character, the backslash itself will be displayed! Therefore, you do not need to escape the backslash itself.
Note:In PHP 3, an E_NOTICE warning is triggered.
Note:Unlike the other two syntaxes, variables and escape sequences in single quotes strings are not replaced by variable values.
<?phpecho 'this is a simple string';echo 'You can also have embedded newlines instrings this way as it isokay to do';// Outputs: Arnold once said: "I'll be back"echo 'Arnold once said: "I'll be back"';// Outputs: You deleted C:*.*?echo 'You deleted C:\*.*?';// Outputs: You deleted C:*.*?echo 'You deleted C:*.*?';// Outputs: This will not expand: n a newlineecho 'This will not expand: n a newline';// Outputs: Variables do not $expand $eitherecho 'Variables do not $expand $either';?> |
Double quotation marks
If double quotation marks (") are used to enclose strings, PHP understands the escape sequence of more special characters:
Table 11-1. escape characters
Sequence meaning
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] {} the regular expression sequence matches a character represented by the octal symbol
X [0-9A-Fa-f] {} this regular expression matches a character in a sequence expressed in hexadecimal notation
In addition, if you try to escape any other characters, the backslash itself will be displayed!
The most important aspect of a double quotation mark string is that the variable name is replaced by the variable value. For details, see string parsing.
Delimiters
Another method is to define the string using the delimiter syntax ("<"). 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.
Warning
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, in a Macintosh system, it is r.
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.
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.
Example 11-2. delimiter string example:
foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); }}$foo = new foo();$name = 'MyName';echo <<
foo.Now, I am printing some {$foo->bar[1]}.This should print a capital 'A': x41EOT;?>
|
Note: The delimiters can be added to PHP 4.
1
String is a series of characters. In PHP, the character is the same as the byte, that is, there are a total of 256 different characters. This also implies that PHP does not support Unicode locally...