PHP section 2 data type: string type

Source: Internet
Author: User
The string type of the PHP Data type, which is generally included in single or double quotation marks.

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

  • Single quotes
  • Double quotation marks
  • Heredoc syntax structure
  • Nowdoc syntax structure (since PHP 5.3.0 and later)
Single quotes

The simplest way to define a string is to enclose it with single quotes (punctuation marks)').

To output a single quotation mark, add a backslash (\). To output a backslash before a single quotation mark or at the end of a string, enter two (\\). Note: If a backslash is added before any other character, the backslash is directly output.

Note: Unlike the double quotation marks and heredoc syntax structure, the variables and special characters in the single quotation mark string willNoReplaced.

If the character string is enclosed in double quotation marks ("), PHP will parse some escape characters, the same as the single quotation mark string. if the character other than the escape character is output, the backslash will be printed. Before PHP5.1.1,\ {$ Var}The backslash in is not displayed yet.

The most important feature of a string defined by double quotation marks is that the variable is executed.

Heredoc structure

The third way to define a string is to use the heredoc syntax structure:<. After this prompt, you need to define an identifier and then a new line. The next step is the string itself. Finally, use the identifier defined above as the end mark.

Identifier referenced at end timeRequiredAt the beginning of a row, the identifier name must follow the PHP rules like other labels: it can only contain letters, numbers, and underscores, and cannot start with numbers or underscores.

Note that the end identifier in additionPossibleThere is a semicolon (;. This means the identifierNot indent, There cannot be any blank or tabs before and after the semicolon. More importantly, the end identifier must be preceded by a New Line label recognized by the local operating system. for example, in UNIX and Mac OS X systems\ NThe end identifier (may have a semicolon) must be followed by a New Line label.

If the end tag is not "clean" due to non-compliance with this rule, PHP considers it not the end identifier and continues searching. If a correct end identifier is not found before the end of the file, PHP will generate a syntax error in the last line.

The Heredoc structure is like a double quotation mark string without double quotation marks. This means that in the heredoc structure, quotation marks are not replaced, but the characters listed above (\ NAnd so on. The variable will be replaced, but be especially careful when strings in the heredoc structure express complex variables.

Nowdoc structure

Just like the heredoc structure is similar to the double quotation mark string, and the Nowdoc structure is similar to the single quotation mark string. The Nowdoc structure is similar to the heredoc structure, but nowdocNo parsing operation. This structure is suitable for PHP code and other large text without escaping. And SGML The structure is similar to the non-resolved text used to declare a large segment, and the nowdoc structure also has the same features.

A nowdoc structure is also marked with the same structure as heredocs<But the sign following it must be enclosed in single quotes, just like<'Eot'In this way. All the rules of the heredocs structure also apply to the nowdoc structure, especially the rules of the End flag.

Variable parsing

When a string is defined using double quotation marks or the heredoc structure, the variation in the string will be parsed.

There are two syntax rules: a simple rule and a complex rule. Simple syntax rules are the most common and convenient. they can be used with the least code to add variables, array values, or object attributes to a string.

Complex syntax rules are added after PHP4, and the expressions surrounded by curly brackets are obviously marked.

Simple syntax rules

When the PHP parser encounters a dollar sign ($), It will form a valid variable name as much as possible like many other resolvers. You can define the boundary of a variable name using the asterisk.

To express more complex structures, use complex syntax rules.

Complex syntax rules

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

This method can be used for any scalar variable, array variable, or object attribute that you want to use in a string. Simply write the expression out of the string, and then use curly brackets.{And}Enclose it. Because{Cannot be escaped, only$Stay close{Can be recognized.{\ $To express{$.

Access and modify characters in a string

The character in the string can be searched and modified using a square brackets similar to the array structure containing numbers starting with 0. for example$ Str [42]Can think of strings as arrays. The substr () and substr_replace () functions can be used to implement more than one character.

The number in the Warning square brackets is out of range and will be blank. The non-integer type is converted to an integer, and the non-integer type is converted to an integer. if the non-integer type is invalid,E_NOTICELevel error. a negative number will generateE_NOTICEBut the read is a null string. The specified string is only available for the first character, and the null string is specified as a null byte.

Useful functions and operators

The string can be connected using the '.' (vertex) operator. Note that the '+' (plus sign) operatorNoThis function.

There are many useful functions for string operations.

You can refer to string functions to learn about most functions. for advanced search and replacement functions, refer to regular expression functions or Perl-type regular expression functions.

In addition, there are URL string functions, as well as encryption/decryption string functions. (Mcrypt and mhash ).

Finally, you can refer to character type functions.

Convert to string

A value can be prefixed by adding(String)Or use the strval () function to convert the string. In an expression that requires a string, the string is automatically converted. for example, when echo or print is used, or when a variable is compared with a string, this transformation type and type conversion can better explain the following, or refer to the function settype ().

A booleanTRUEThe value is converted to a string."1". BooleanFALSEConverted""(Empty string ). This transformation can be performed between a boolean and a string.

A string (including the exponent part of a floating point number) that is converted into a numeric literal style (4.1E + 6.

Convert an array to a string"Array"Therefore, echo and print c cannot display the value of this array. If an array value is displayed, you can useEcho $ arr ['foo']For more information about this structure, see the following.

In PHP 4, objects are converted to strings."Object"To debug, you need to print the object value. for the method, see the body. To get the class name of the object, you can use the get_class () function. In PHP5, _ toString can be used.

Resources are always converted"Resource id #1"String of this structure, where1Is a unique number allocated to the resource by PHP. You don't need to pay too much attention to this structure. it is about to change. To obtain a resource type, you can use the get_resource_type () function ().

NULLIt is always converted into an empty string.

As mentioned above, directly converting an array, object, or resource into a string does not get more information than itself. You can use the print_r () and var_dump () functions to list these types of content.

Most PHP values can be converted into string s for long-term storage. this is called serialization and can be implemented using the serialize () function. If the PHP engine supports WDDX, PHP values can also be stored in XML format.

Convert string to numeric

When a string is used in a numeric environment, the result and type are as follows:

If the string does not contain '.', 'e', or 'e' and the numeric value meets the integer type limitation (PHP_INT_MAXDefined). This string can be considered as an integer and is considered as a float in other cases.

The start part of the string is given its value. if the string starts with a valid number, this number can be used directly. Otherwise, the value is 0 (0 ). A valid value is a symbol followed by one or more digits (which may have a decimal point), followed by an optional index symbol, such as 'E' or 'e ', followed by one or more numbers.

Do not use an integer to convert the corresponding characters in the C language. use the functions ord () and chr () to convert the ASCII code and characters.

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.