PHP numeric type String type detailed _php tutorial

Source: Internet
Author: User
In PHP, a character string that we commonly use is single quotes and double quotes before the syntax structure Heredoc and nowdoc are defined, both of which are to be in the php5.3.0 version above.

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. Before PHP5.1.1, the backslash in {$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 note that the end identifier line may not include a semicolon (;), except for 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 that is recognized by the local operating system, such as N 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 without double quotes, which means that the quotes are not replaced in the HEREDOC structure, but the characters listed above (n, and so on) are also used. 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 heredocs structure, but the following markers are enclosed in single quotes, like the <<< ' 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, with brackets in a similar array structure containing corresponding numbers, such as $STR [42], which can be used to visualize the string as an array. 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 an E_notice level error, negative numbers produce a e_notice when written, 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, this type of transformation and the type conversion can better explain the following, or refer to the function Settype ().


A Boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (empty string). 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), and the floating-point numbers (4.1E+6) using exponential notation 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 ().

Null is 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 qualification of the integer type (Php_int_max defined), the string can be considered an integer and in other cases is considered 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.


Here are some common string handling functions to share


Addslashes: String joins a slash.
Bin2Hex: binary turns into 16 decimal.
Chop: Remove continuous whitespace.
CHR: The character that returns the ordinal value.
Chunk_split: Divides the string into small segments.
Convert_cyr_string: Converts an old Slavic string into another string.
Crypt: The string is encrypted with DES encoding.
Echo: the output string.
Explode: Cuts the string.
Flush: Clear out the output buffer.
Get_meta_tags: Extract data from all meta tags of the file.
Htmlspecialchars: Converts special characters into HTML format.
Htmlentities: Turns all characters into HTML strings.
Implode: Turns the array into a string.
Join: Turns the array into a string.
LTrim: Remove continuous whitespace.
MD5: Computes the MD5 of the string.
NL2BR: Converts a newline character to

ORD: Returns the ordinal value of a character.
PARSE_STR: Parses the query string into a variable.
Print: Output string.
printf: Output formatted string.
Quoted_printable_decode: Converts a QP encoded string into a 8-bit string.
Quotemeta: Add reference symbol.
Rawurldecode: Restore from a URL-specific format string to a normal string.
Rawurlencode: Encodes a string into a URL-specific format.
SetLocale: Configure geo-information.
Similar_text: Calculates the similarity of strings.
Soundex: Calculating the phonetic value of a string
sprintf: Formats the string.
STRCHR: Look for the first occurrence of the character.
strcmp: string comparison.
STRCSPN: The length of different strings.
Strip_tags: Remove HTML and PHP markup.
Stripslashes: Remove the backslash character.
Strlen: Gets the string length.
Strrpos: Looks for a character in the string where the last occurrence occurs.
Strpos: Looks for a character in a string that appears first.
STRRCHR: Gets the string that appears at the last occurrence of a character.
Strrev: Reverses the string.
STRSPN: Find out how many strings fall in another string mask.
Strstr: Returns the string from the beginning of the string to the end of a string.
Strtok: Cuts the string.
Strtolower: All strings are converted to lowercase.
Strtoupper: All strings are capitalized.
Str_replace: String substitution.
STRTR: Converts some characters.
SUBSTR: Takes a partial string.
Trim: Truncate the space between the end of the string.
Ucfirst: capitalizes the first character of the string.
Ucwords: capitalizes the first letter of each word in a string.

http://www.bkjia.com/PHPjc/629163.html www.bkjia.com true http://www.bkjia.com/PHPjc/629163.html techarticle in PHP, a character string that we commonly use is single quotes and double quotes before the syntax structure Heredoc and nowdoc are defined, both of which are to be in the php5.3.0 version above. One character in PHP ...

  • 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.