Home  >  Article  >  Backend Development  >  Detailed explanation of string type of php number type_PHP tutorial

Detailed explanation of string type of php number type_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:24942browse

In PHP, you can character a string. What we commonly use are the syntax structures behind single quotes and double quotes, heredoc and nowdoc definitions. These two must be in PHP5.3.0 or above.

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

Single quotes
Double quotes
heredoc syntax structure
nowdoc syntax structure (since PHP 5.3.0)
Single quote
The simplest way to define a string is to surround it with single quotes (punctuation mark ').

If you want to output a single quote, you need to add a backslash () in front of it. To output a backslash before a single quote or at the end of a string, enter two (). Note that if a backslash is preceded by any other character, the backslash will be output directly.

Note: Unlike double quotes and heredoc syntax constructs, variables and special meaning characters in single quoted strings will not be replaced.

If the string is surrounded by double quotes ("), PHP will parse some escape characters, just like the single quote string. If the characters other than the escape characters are output, the backslash will be printed. . Before PHP5.1.1, the backslash in {$var} was not displayed.

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 structure: <<<. After the prompt, define an identifier, followed by a new line. Next comes the string itself, ending with the previously defined identifier.
The identifier quoted at the end of

must be at the beginning of the line. Moreover, the naming of identifiers must follow the same PHP rules as other tags: they can only contain letters, numbers, and underscores, and numbers and underscores cannot be used as beginning.

Warning: It should be noted that the end identifier line must not include other characters except possibly a semicolon (;). This means that identifiers cannot be indented, nor can there be any whitespace or tabs before or after a 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, and the end identifier (which may have a semicolon) must also be followed by New line label.

If this rule is not followed and the closing tag is not "clean", PHP will think it is not a closing identifier and continue looking. 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 without the use of double quotes. This means that the quotes are not 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 a heredoc structure.

Nowdoc structure

Just like the heredoc structure is similar to a double-quoted string, the Nowdoc structure is similar to a single-quoted string. The Nowdoc structure is very similar to the heredoc structure, but nowdoc does not perform parsing operations. This structure is suitable for PHP code and other large pieces of text that do not need to be escaped. Similar to SGML's structure, which is used to declare large sections of text that do not need to be parsed, the nowdoc structure also has the same characteristics.

A nowdoc structure also uses the same tags <<< as a heredocs structure, but the following identifier must be enclosed in single quotes, like <<<'EOT'. All the rules for the heredocs structure also apply to the nowdoc structure, especially the rules for closing identifiers.

Variable analysis

When a string is defined with double quotes or a heredoc structure, the variables in it will be parsed.

There are two kinds of grammar rules here: a simple rule and a complex rule. Simple syntax rules are the most common and convenient, allowing you to add variables, array values, or object properties to a string with minimal code.

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

Simple syntax rules

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

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

Complex syntax rules

Complex syntax rules are not named because of their complex structure, 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 you would outside a string, then surround it with curly braces { and }. Since { cannot be escaped, only $ will be recognized if it is next to {, so {$ can be used to express {$.

Access and modify characters in strings

The characters in the string can be found and modified by starting with 0 and containing the corresponding number in square brackets similar to the array structure, such as $str[42]. You can think of the string as an array. The functions substr() and substr_replace() can be used to implement more than one character.


WarningNumbers in square brackets that are out of range will produce blanks. Non-integer types are converted to integers, and non-integer types are converted to integers. Illegal types will generate an E_NOTICE level error. Negative numbers will generate an E_NOTICE when written, but an empty string will be read. Only the first character of the specified string is available, and an empty string is specified as a null byte.

Useful functions and operators
Strings can be concatenated using the '.' (dot) operator. Note that the '+' (plus sign) operator does not have this functionality.

There are many useful functions for string operations.

You can refer to String Functions to learn about most functions. For advanced search & replace functions, please refer to Regular Expression Functions or Perl-type regular expression functions.

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

Finally, you can refer to character type functions.


Convert to string

A value can be converted into a string by preceding it with (string) or using the strval() function. In an expression that requires a string, the string is automatically converted, such as when using the function echo or print, or when a variable is compared with a string. This conversion type and type conversion can be better For an explanation of the following things, please also refer to the function settype().


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


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

The array is converted into the string "Array", therefore, echo and print c cannot display the value of the array. If you want to display an array value, you can use the echo $arr['foo'] structure, more on that below.

In PHP 4, the object is converted into the string "Object". For debugging reasons, the value of the object needs to be printed out. See the text for the method. To get the name of an object's class, use the get_class() function. In PHP5, you can use __toString.

Resources will always be converted into a string with the structure "Resource id #1", where 1 is the unique number assigned to the resource by PHP. Don't pay too much attention to this structure, it is about to change. To get a resource type, use the function get_resource_type().

NULL is always converted to an empty string.

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

Most PHP values ​​can be converted into string s for long-term storage. This is called serialization and can be achieved using the function serialize(). If the PHP engine is configured to support WDDX, PHP values ​​can also be stored in XML format.

Convert string to number
When a string is used in a numeric context, 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 restrictions (defined by PHP_INT_MAX), the string can be considered an integer, otherwise it is considered an integer a float.

The starting part of the string gives its value. If the string starts with a legal number, this number can be used directly. Otherwise, the value is 0 (zero). Legal values ​​consist of 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.

Don’t imagine getting the corresponding characters through an integer conversion like in C language. Use the functions ord() and chr() to convert between ASCII codes and characters.


Here are some commonly used string processing functions


AddSlashes: Add slashes to the string.
bin2hex: Convert binary to hexadecimal.
Chop: Remove consecutive blanks.
Chr: Returns the ordinal value of a character.
chunk_split: Divide the string into small segments.
convert_cyr_string: Convert Cyrillic strings to other strings.
crypt: Encrypt the string using DES encoding.
echo: output string.
explode: split the string.
flush: Clear the output buffer.
get_meta_tags: Extract the data of all meta tags in the file.
htmlspecialchars: Convert special characters to HTML format.
htmlentities: Convert all characters into HTML strings.
implode: Convert an array into a string.
join: Convert an array into a string.
ltrim: Remove consecutive whitespace.
md5: Computes the MD5 hash of a string.
nl2br: Convert newline characters to
.
Ord: Returns the ordinal value of a character.
parse_str: Parse query string into variables.
print: output string.
printf: output formatted string.
quoted_printable_decode: Convert qp encoded string into 8-bit string.
QuoteMeta: Add quote symbols.
rawurldecode: Convert URL-specific format string to normal string.
rawurlencode: Encode a string into a URL-specific format.
setlocale: Configure localization information.
similar_text: Calculate string similarity.
soundex: Calculate the pronunciation value of a string
sprintf: Format strings.
strchr: Find the first occurrence of a character.
strcmp: String comparison.
strcspn: length of different strings.
strip_tags: Remove HTML and PHP tags.
StripSlashes: Strips backslash characters.
strlen: Get the length of the string.
strrpos: Find the last occurrence of a character in a string.
strpos: Find the first occurrence of a character in a string.
strrchr: Get the string starting from the last occurrence of a character.
strrev: Reverse a string.
strspn: Find the number of times a string falls within the mask of another string.
strstr: Returns the string from the beginning to the end of a string in the string.
strtok: Cut a string.
strtolower: Convert all strings to lowercase.
strtoupper: Convert the string to all uppercase letters.
str_replace: String replacement.
strtr: Convert certain characters.
substr: Get part of the string.
trim: Trim the spaces at the beginning and end of the string.
ucfirst: Change the first character of the string to uppercase.
ucwords: Capitalize the first letter of each word in the string.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629163.htmlTechArticleIn php, you can character a string. What we commonly use are single quotes and double quotes. The syntax structures behind heredoc and Nowdoc defines that these two must be in php5.3.0 or above. A character in PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn