PHP section II data type String type _php tutorial

Source: Internet
Author: User

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

    • single quotation marks
    • Double quotes
    • HEREDOC Syntax Structure
    • NOWDOC Syntax Structure (Since PHP 5.3.0)

Single quotation marks

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

If you want to output a single quotation mark, precede it with a backslash (\). If you want to output a backslash before the single quotation mark or at the end of the string , enter two (\ \). Note that if the backslash is preceded by any other character, the backslash will be output directly.

Note: Unlike double quotes and heredoc syntax structures, variables in single-quote strings and special meanings of characters will not be replaced.

If the string is enclosed in double quotation marks ("), PHP will parse some escape characters and, like a single quote string , the backslash will be printed if characters other than the escape character are output. PHP5.1.1 previously, the backslash in the\{$var} was not displayed.

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

Heredoc structure

The third way to define a string is to use the HEREDOC syntactic structure:<<<. After the prompt, you define an identifier and then a new line. The next is the string itself, and finally the identifier defined earlier as the end flag.

The identifier referenced at the end must be at the beginning of a line, and the name of the identifier should follow the rules of PHP like any other label: it can only contain letters, numbers, and underscores, and cannot begin with numbers and underscores.

Warning to note is the end identifier line except that there may be a semicolon (;). Must not include other characters. This means that the identifier cannot be indented , and the semicolon cannot have any whitespace or tabs before or after it. More importantly, the end identifier must be preceded by a new row label recognized by the local operating system, such as in Unix and Mac OS x systems , and the end identifier (possibly a semicolon) followed by a new row label.

If you do not follow this rule and the end tag is not "clean", PHP will assume that it is not the end identifier and continue to look for it. If a correct end identifier is not found before the end of the file, PHP will produce a syntax error on the last line.

The HEREDOC structure is like a double-quote string that does not use double quotes, which means that the quotes are not replaced in the HEREDOC structure, but the characters listed above (\ n , etc.) are also available. The variables will be replaced, but be careful when the strings Express complex variables in the HEREDOC structure.

Nowdoc structure

Just as the heredoc structure resembles a double-quote string, the Nowdoc structure is similar to a single-quote string. The NOWDOC structure is much like the heredoc structure, but Nowdoc does not perform parsing operations . This structure is well suited for use in PHP code and other large pieces of text that do not need to be escaped. The structure of SGML is used to declare a large segment of text that is not parsed, and the NOWDOC structure has the same characteristics.

A NOWDOC structure is also labeled <<<with the same structure as the Heredocs, but the following glyphs are enclosed in single quotes, like <<< ' EOT ' . All rules of the HEREDOCS structure also apply to the NOWDOC structure, especially the rule of the end marker.

Variable resolution

When a string is defined by a double-quote or HEREDOC structure, the variable is parsed.

There are two grammatical rules: a simple rule, a complex rule. Simple sentence rules are the most commonly used and most convenient, it can use a minimum of code in a string to add variables, array values, or object properties.

Complex sentence rules are added after PHP4, the expression surrounded by curly braces is its obvious mark.

The rules of simple sentences

When the PHP parser encounters a dollar sign ($), it will, like many other parsers, try to form a valid variable name. You can use curly braces to clarify the line of variable names.

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

The rules of complex sentences

Complex sentence rules are not named for complex structures, but because they can use complex expressions.

You can use this method for any variable that you want to use in a string , an array variable, or an object property. Simply write the expression as if it were outside the string and enclose it in curly braces { and } . { \$ can be used to express { $} because { cannot be escaped, only $ is recognized next to it.}

Accessing and modifying characters in a string

The characters in the string can be found and modified by a 0-based, square bracket containing the corresponding number in a similar array structure, for example $str[42] , to imagine an array of strings . The function substr () and substr_replace () can be used to implement more than one character case.

warning the number in square brackets out of range will result in whitespace. Non-integer types are converted to integers, non-integer types convert to integers, illegal types produce a E_NOTICE level error, negative numbers produce one when written E_NOTICE , but empty strings are read. The string specified is only the first character available, and the empty string is specified as an empty byte.

Useful functions and operators

strings can be used '. ' (dot) operator, note that the ' + ' (plus) operator does not have this function.

There are a number of useful functions for manipulating strings .

You can refer to the string function for most functions, and the Advanced Find & Replace feature can refer to regular expression functions or Perl type regular expression functions.

There are also functions for URL strings , as well as functions to encrypt/decrypt strings. (mcrypt and mhash).

Finally, you can refer to the character type function .

Convert to String

A value can be converted to a string by adding (string) before it or by using the strval () function. In an expression that requires a string , the string is automatically transformed, such as when you use the function echo or print , or when a variable and a string are compared. It's going to happen. This type of transformation and type conversion can better explain the following things, but also refer to function settype () .

A boolean TRUE value is converted to the string "1". Boolean is converted to "" (empty string). FALSE This transition can occur between a Boolean and a string .

An integer or floating-point number is converted to a literal string of numbers (including the exponential portion of the float ), using the exponential notation of floating-point numbers (4.1E+6 ) can also be transformed.

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

In PHP 4, the object is converted to the string "Object", for debugging reasons need to print out the value of the object, see the body of the method. In order to get the name of the class of the object, you can use the get_class () function. In PHP5, you can use __tostring .

Resources are always transformed into strings of the structure "Resource ID #1" , where 1 is the unique number that PHP assigns to the resource. Instead of paying too much attention to this structure, it's going to change right away. In order to get a resource type, you can use the function Get_resource_type ().

NULLis always transformed into an empty string.

As mentioned above, converting an array , object , or resource directly into a string does not get more information beyond itself. These types of content can be listed using Functions print_r () and var_dump () .

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

The string is transformed into a number

When a string is used in the context of 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 PHP_INT_MAX qualification of the integer type (defined), the string can be determined to be an integer, In other cases is identified as a float.

The starting part of the string is given its value, and if the string starts with a valid number, the number can be used directly. Otherwise, the value is 0 (0). The legal value is represented by the symbol, followed by one or more digits (there may be a decimal point), followed by an optional exponent symbol such as ' e ' or ' e ', followed by one or more digits.

Do not imagine that in the C language, through an integer conversion to get the corresponding characters, using the function ord () and Chr () to achieve the conversion between ASCII code and characters.

http://www.bkjia.com/PHPjc/325303.html www.bkjia.com true http://www.bkjia.com/PHPjc/325303.html techarticle a string in PHP can be defined in the following 4 ways: Single quotation mark HEREDOC syntax structure NOWDOC syntax structure (since PHP 5.3.0) Single quotation marks define a 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.