Php numeric string type

Source: Internet
Author: User
Tags chr numeric md5 numeric value ord php code

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 ').

If you want to output a single quotation mark, you must add a backslash () before it (). 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 double quotes and heredoc syntax structures, variables and special characters in single quotes strings are not replaced.

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, the backslash in {$ var} 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.

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

Note that the end identifier line must not contain any other characters except a semicolon. This means that the identifier cannot be indented, and 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, such as n in UNIX and Mac OS X systems, the end identifier (which 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 the quotation marks in the heredoc structure do not need to be replaced, but the characters listed above (n, etc.) can also be used. 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 the nowdoc does not parse the structure. This structure is suitable for PHP code and other large text without escaping. And SGML <! [CDATA []> The structure is similar to the non-parsed text used to declare large segments, and the nowdoc structure also has the same features.

A nowdoc structure is also marked with the same structure as heredocs <, but the symbol following it must be enclosed in single quotes, just like <'eot. 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 try to form a valid variable name 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 as it is outside the string and enclose it with braces {and. Because {cannot be escaped, only $ stands close to {will be recognized. You can use {$ 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 the corresponding numbers, such as $ str [42], you 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. If the non-integer type is converted to an integer, an E_NOTICE level error is generated if the non-integer type is invalid. If the negative number is written, an E_NOTICE is generated, but the read value 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) operator does not have this 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 converted into a string by adding (string) or using the strval () function before it. 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 boolean TRUE value is converted to the string "1 ". Boolean FALSE is converted to "" (an empty string ). This transformation can be performed between a boolean and a string.


An integer or floating point number is converted to a numeric literal string (including the exponent part of a floating point number), and can also be converted using a floating point number (4.1E + 6) of the exponential notation.

The Array is converted to the string "Array". Therefore, echo and print c cannot display the value of this Array. If an array value is displayed, use the echo $ arr ['foo'] structure. For more information, see the following.

In PHP 4, the Object is converted to the string "Object". For debugging reasons, 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.

The Resource is always converted into a string of the "Resource id #1" structure, where 1 is the 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 ().

NULL 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 limitation of the integer type (defined by PHP_INT_MAX). This string can be considered as an integer, in other cases, it is regarded as a float.

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.


Below are some common string processing functions.


AddSlashes: Adds a slash to the string.
Bin2hex: binary to hexadecimal.
Chop: removes consecutive gaps.
Chr: returns the character of the ordinal value.
Chunk_split: splits a string into segments.
Convert_cyr_string: convert the ancient Slavic string to another string.
Crypt: encrypt the string with DES encoding.
Echo: output string.
Explode: cut string.
Flush: clears the output buffer.
Get_meta_tags: extracts all metadata marked by meta from the file.
Htmlspecialchars: Convert Special characters into HTML format.
Htmlentities: converts all characters into HTML strings.
Implode: converts an array into a string.
Join: converts an array into a string.
Ltrim: removes consecutive white spaces.
Md5: calculate the MD5 of the string.
Nl2br: converts a line break to <br>.
Ord: returns the ordinal value of a character.
Parse_str: parses the query string into a variable.
Print: output string.
Printf: output the formatted string.
Quoted_printable_decode: convert an qp encoded string to an 8-bit string.
QuoteMeta: add a reference symbol.
Rawurldecode: restores a string from a URL-specific format to a normal string.
Rawurlencode: encode a string into a URL-specific format.
Setlocale: configure the region information.
Similar_text: string similarity calculation.
Soundex: calculates the pronunciation of a string.
Sprintf: format the string.
Strchr: find the first occurrence character.
Strcmp: string comparison.
Strcspns: the length of different strings.
Strip_tags: removes HTML and PHP tags.
StripSlashes: removes the backslash.
Strlen: Gets the string length.
Strrpos: find the last occurrence of a character in the string.
Strpos: searches for the first occurrence of a character in a string.
Strrchr: Gets the string starting from the last occurrence of a character.
Strrev: Reverse string.
Strspns: identifies the number of masks that a string falls into another string.
Strstr: returns the string from the beginning to the end of a string.
Strtok: cut string.
Strtolower: converts all strings to lowercase letters.
Strtoupper: converts all strings to uppercase.
Str_replace: string replacement.
Strtr: converts certain characters.
Substr: obtains some strings.
Trim: removes spaces at the beginning and end of a string.
Ucfirst: uppercase for the first character of the string.
Ucwords: change the first letter of each character in the string to uppercase.

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.