Section 2. String types of data types

  • 2020-05-16 06:31:45
  • OfStack

A string in PHP can be defined in the following four ways:

Single quotes Double quotation marks heredoc syntax nowdoc syntax (since PHP 5.3.0) Single quotes

The easiest way to define a string is to enclose it in single quotation marks (punctuation ').

If you want to output a single quote, put a backslash (\) before it. To output a backslash before a single quote or at the end of a string, type two (\\). Note that if you put a backslash before any other character, the backslash will be printed directly.

Note: unlike double quotes and heredoc syntax structures, variables and characters with special meanings in single quoted strings will not be replaced.

If the string is enclosed in double quotation marks ("), PHP will parse some escape characters, like the single-quotation mark string 1. If any characters other than escape characters are output, the backslash will be printed. The backslash in \{$var} will not be displayed until PHP5.1.1.

The most important feature of a string defined with double quotes is that the variable will be executed.

Heredoc structure

The third way to define a string is to use the heredoc syntax: < < < . After the prompt, you define an identifier, followed by a new line. Next comes the string itself, ending with the identifier defined earlier.

The identifier referenced at the end must be at the beginning of line 1, and the identifier names must follow PHP's rules like any other tag 1: only letters, Numbers, and underscores can be included, and no Numbers or underscores can be used as the beginning.

Warning note that the end identifier line may have a semicolon (;) Other characters must not be included. This means that the identifier cannot be indented, nor can there be any white space or tabs before or after the semicolon. More importantly, the end identifier must be preceded by a new line label that is recognized by the local operating system, such as \n in UNIX and Mac OS X, and the end identifier (which may have a semicolon) must be followed by a new line label.

If non-compliance with this rule results in an end tag that is not "clean," PHP will consider it not an end identifier and continue looking for it. If a correct end identifier is not found before the end of the file, PHP will generate a syntax error on the last line.

The Heredoc structure is like a double quoted string that does not use double quotes, which means that the quotes do not have to be replaced in the heredoc structure, but the characters listed above (\n, etc.) can also be used. Variables will be replaced, but be careful when expressing complex variables as strings in an heredoc structure.

Nowdoc structure

Just as the heredoc structure is similar to the double-quoted string, the Nowdoc structure is similar to the single-quoted string. The Nowdoc structure is similar to the heredoc structure, but nowdoc does not parse. This structure is ideal for PHP code and other large pieces of text that do not need to be escaped. With SGML < ![CDATA[ ]] > Structures are used to declare large chunks of text without parsing. Similarly, nowdoc structures have the same characteristics.

An nowdoc structure is also marked like an heredocs structure < < < , but the tags that follow should be enclosed in single quotation marks, like < < < 'EOT'. All the rules of the heredocs structure also apply to the nowdoc constructs, especially the rules of the end token.

Variable resolution

When a string is defined in double quotation marks or an heredoc structure, the variables in it are parsed.

There are two grammatical rules: one simple rule and one complex rule. Simple syntactic rules are the most common and convenient, which allow you to add variables, array values, or object properties to a string with a minimum of code.

Complex syntactic rules are added after PHP4, and expressions surrounded by curly braces are obvious markers.

Simple syntactic rule

When the PHP parser encounters a dollar sign ($), it tries to form a valid variable name, as many other parsers do. You can use a bracket sign to define the boundaries of variable names.

If you want to express more complex structures, use complex syntactic rules.

Complex syntactic rule

Complex syntax rules are named not because they are complex, but because they can use complex expressions.

Any scalar variable, array variable, or object property that you want to use in a string can use this method. Simply write the expression as if it were outside of the string, and enclose it with curly braces {and}. Since {cannot be escaped, only $must be next to {to be recognized. {$can be expressed as {\$.

Access and modify characters in a string

Characters in a string can be found and modified by starting with a 0 and using square brackets containing the corresponding Numbers in an ararray structure, such as $str[42], which can be thought of as an array. The functions substr() and substr_replace() can be used for situations with more than one character.

The number in Warning square brackets that goes out of range will be blank. Non-integer types are converted to integers, and non-integer types are converted to integers. Illegal types will generate 1 E_NOTICE level error. Negative Numbers will generate 1 E_NOTICE, but will read an empty string. Only the first character of the specified string is available, and the empty string is specified as a null byte.

Useful functions and operators

Strings can be concatenated with the '.' (dot) operator; note that the '+' (plus) operator does not.

There are many useful functions for manipulating strings.

You can refer to string functions for most functions, and the advanced look-and-replace functionality can refer to regular expression functions or regular expression functions of type Perl.

There are also functions for URL strings, as well as for encrypting/decrypting strings. (mcrypt and mhash).

Finally, you can refer to the character type function.

Convert to a string

A value can be converted to a string by prefacing it with (string) or using the strval() function. In an expression that requires a string, the string is automatically converted, such as when the functions echo or print are used, or when 1 variable is compared to 1 string. This conversion type and type conversion can better explain the following, and can also refer to the function settype().

1 boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (empty string). This conversion can be made between boolean and the string.

An integer or floating point number is converted to the literal style string of a number (including the exponent part of the floating point number), as is a floating point number using the exponent counting method (4.1E+6).

The array is converted to the string "Array", so echo and print c cannot display the value of the array. If you display an array value, you can use the structure echo $arr['foo'] for more information.

In PHP 4 the object is converted to the string "Object". The value of the object needs to be printed out for debugging purposes. To get the name of the object's class, you can use the get_class() function. S: in PHP5, you can use s 186en.

The resource is always converted to the string "Resource id #1", where 1 is the unique number assigned to the resource by PHP. Don't worry too much about the structure, it's about to change. To get one resource type, use the function get_resource_type().

NULL is always converted to an empty string.

As mentioned above, converting an array, object, or resource directly to a string does not yield more information than it would otherwise have. You can list these types of content using the functions print_r() and var_dump().

Most of the PHP values can be converted to the string s for long-term storage, which is called serialization and can be implemented using the function serialize(). If the PHP engine is configured to support WDDX, the PHP value can also be stored in XML.

Turn the string into a number

When a string is used with a number, the result and type are as follows:

If the string does not contain '.', 'e' or 'E' and the numeric value conforms to the integer type qualification (PHP_INT_MAX definition), this string can be identified as an integer and in other cases as an float.

The beginning of a string is given its value, and if the string starts with a valid number, this number can be used directly. Otherwise, the value is 0 (zero). A valid value is followed by a symbol followed by one or more digits (possibly with a decimal point), followed by an optional exponent symbol such as 'e' or 'E' followed by one or more digits.

Instead of converting from one integer to the corresponding character as you would in C, use the functions ord() and chr() to convert between the ASCII code and the characters.


Related articles: