: This article mainly introduces the type-PHP manual notes. For more information about PHP tutorials, see. Type Overview
PHP supports eight types of raw data.
To view the value and type of an expression, usevar_dump()
Function.
The above code passes throughvar_dump()
The output result is as follows (PHP version 5.5.12 ).
array (size=3) 0 => int 1 1 => int 2 2 => array (size=3) 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1)float 3.1boolean true
If you want to use a simple expression for debugging, usegettype()
Function. Do not usegettype()
And useis_type
Function. Useis_type
You can filter parameters.
If you want to forcibly convert a variable to a certain type, you can use the force conversion orsettype()
Function. Note that the variable will show different values in specific situations based on its current type.
Boolean type
Note that when converting to boolean, the following values are considered FALSE:
Empty string and string "0"
Array that does not contain any elements
If it is an object, the return value is TRUE.
The following program can deepen the understanding of boolean type conversion.
Integer
To use the octal expression, add 0 (zero) before the number ).
To use a hexadecimal expression, you must add 0x before the number.
To use a binary expression, you must add 0 B before the number.
Integer expressed in binary format is available in PHP 5.4.0.
The length of an integer value can be a constant.PHP_INT_SIZE
The maximum value can be a constant.PHP_INT_MAX
.
The last statement of the above program, a strange thing occurred when processing gossip. That's because if an invalid number (8 or 9) is passed to the octal number, the remaining digits are ignored.
If a given number exceeds the integer range, it is interpreted as float. Similarly, if the execution result exceeds the integer range, float is returned.
PHP does not have the division operator. 1/2 generates float 0.5. The value can be forcibly converted to an integer instead of the decimal part, orround()
Functions can be better rounded.
As stated in the manual, it is never necessary to forcibly convert an unknown score to an integer, which sometimes leads to unpredictable results.
The output result of this program is 7. never trust floating point numbers!
Float
Some mathematical operations produce a constantNAN
Result. This result represents an undefined or undefined value in the floating point number operation. The result of any loose or strict comparison between this value and any other value is FALSE.
String
A string is composed of a series of characters, each of which is equivalent to a byte. The implementation is to add an integer to an array composed of bytes to specify the buffer length. This means that PHP only supports the 256 character set and therefore does not support Unicode. How is Chinese displayed? First, record the questions.
There are 4 syntax expressions in the string:
Single quotes
Double quotation marks
heredoc
nowdoc
Single quotes
To express a single quotation mark, you must add a backslash (\) before it to escape it.
To express a backslash itself, use two backslash (\).
Any other method of backlash will be treated as the backslash itself.
However, if there is only one backslash in the string enclosed in single quotes, the backslash will also be output. why?
Double quotation marks
Escape characters can be parsed. The most important feature is that variables are parsed.
Single quotation marks and double quotation marks all support multi-line string input.
Heredoc structure
This type of structure is rarely used in previous programming. here we will learn in detail.
The structure is roughly as follows:
Operator<<<
Provide the identifier, and then wrap the line.
Next is the string itself.
The identifier defined above is used as the end mark.
The identifier referenced at the end must be in the first column of the row, which means that the identifier cannot be indented. this line may have a semicolon (;) and cannot contain any other character.
The Heredocs structure cannot be used to initialize class attributes. Since PHP 5.3, this restriction is only valid when heredoc contains variables.
The Heredoc structure is like a double quotation mark string without double quotation marks. the escape rules are the same as double quotation marks.
Nowdoc structure
The structure of nowdoc is similar to that of heredoc, but it only follows the operator<<<
The following identifiers must be enclosed in single quotes.
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. No parsing operation is performed in nowdoc. This structure is suitable for Embedding PHP code or other large text without escaping special characters. The nowdoc structure can be used in any static data environment. The most typical example is to initialize attributes or constants of a class.
Variable parsing
There are two syntax rules for variable parsing: simple rules and complex rules. Simple rules are the most common and convenient. here we will learn more about complex syntax rules. A notable mark of complex rule syntax is an expression enclosed by curly brackets.
Complex syntax is not named because of its complex syntax, but because it can use complex expressions. Because {cannot be escaped, it is recognized only when $ is next.
Access and modification
You can access a string as an array. writing with a subscript that exceeds the string length will lengthen the string and fill it with spaces. Non-integer subscript is converted to an integer. Only the first character of the value string is used for writing. If an empty string is assigned a value, the value is NULL. The PHP string is an array of Bytes. Therefore, using curly braces to access or modify strings is not safe for multi-byte character sets.
The string can be connected using the '.' (vertex) operator. Note that the '+' (plus sign) operator does not have this function.
The TRUE value of a boolean value is converted to the "1" of the string ". The FALSE value of boolean is converted to "" (null string ). The start part of the string determines its value. This value is used if the string starts with a valid value. Otherwise, the value is 0 (0 ).
The advantages of PHP are reflected in the convenience of string processing. There are many useful functions for string operations, including various functions and regular expressions.
PHP does not specify the encoding of a string. the string will be encoded in the same encoding method as the script file. Therefore, the function that operates the text must assume how the string is encoded. Unfortunately, PHP has many variants on this function, and you need to learn more about the PHP string processing function.
Array
Arrays in PHP are actually an ordered ing, and valuing is a type that associates values with keys.
You can use the array () language structure to create an array. It accepts any number of comma-separated "key => value pairs ". You can use the short array definition syntax from 5.4 and replace array () (). Key can be integer or string, and value can be of any type.
Key has the following mandatory conversion and rules:
A string containing valid integer values is converted to an integer.
The floating point will also be converted to an integer, meaning that the fractional part of the floating point will be removed.
The Boolean value is also converted to an integer.
Null is converted to a null string, that is, the key name null is actually stored "".
Arrays and objects cannot be used as key names.
If the same key name is used for multiple cells in the array definition, only the last one is used, and the previous one is overwritten.
If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one.
'a', '02' => 'b');var_dump($a);
The above rules show that the output result of this code is as follows:
array (size=2) 20 => string 'a' (length=1) '02' => string 'b' (length=1)
If square brackets are given but no key name is specified, the current maximum integer index value is taken. the new key name is the value plus 1 (but the minimum value is 0 ). To delete a key-value pair, callunset()
Function, which allows you to delete a key in the array, but note that the array will not be re-indexed.
'b');$a[] = 'c';var_dump($a);unset($a[1]);var_dump($a);
For the above program, the output is:
array (size=3) 0 => string 'a' (length=1) 'b' => string 'b' (length=1) 1 => string 'c' (length=1)array (size=2) 0 => string 'a' (length=1) 'b' => string 'b' (length=1)
Always put quotation marks on the array index represented by a string. For example$foo['bar']
Instead$foo[bar]
. In this code, there is an undefined constant (bar) instead of a string ('bar'-note quotation marks), and PHP may define this constant later.
The foreach control structure is specially used for arrays. It provides a simple method to traverse arrays.
The following example program fills the array by reading the directory, which involves the use of several functions.
For any integer, float, string, boolean, and resource types, if you convert a value to an array, an array with only one element is obtained, and its subscript is 0, this element is the value of this scalar.
Object
To create a new object, use the new statement to instantiate a class.
If you convert an object to an object, it will not change. If any other type of value is converted to an object, an instance of stdClass is created. If the value is NULL, the new instance is NULL. Converting an array to an object makes the key name a property name and has a corresponding value. For any other value, the member variable named scalar will include this value.
do_foo();var_dump($bar);$obj = (object) 'hello';var_dump($obj);
The output result of the above sample program is:
Doint foo.object(foo)[1]object(stdClass)[2] public 'scalar' => string 'hello' (length=5)
Resource type
Resource is a special variable that stores a reference to an external resource. All external resources used by resources will be released by the garbage collection system, and few need to be manually released. However, persistent database connections are special and will not be destroyed by the garbage collection system.
NULL
The NULL type has only one value, that is, the case-insensitive constant NULL.
Callback type
The first time I came into contact with this concept, it was a little strange.
Callable can be used to specify the callback type callback from PHP 5.4.
Many examples are used in the manual.call_user_func
Function. let's take a look at this function.call_user_func
Use the first parameter as the callback function and the other parameters as the callback function parameters. Returns the return value of the callback function. If an error occurs, FALSE is returned.
Callback functions can be simple functions, object methods, and static class methods. The following describes how to use callback functions:
Call a user-defined simple function and pass its name as a string.
Call static member methods of a class. classes and methods are passed as arrays. subscript 0 contains the object, and subscript 1 contains the method name.
Call the methods of instantiated objects.
Call static class methods.
Call the static member method of the parent class.
Except for common user-defined functions,create_function()
It can be used to create an anonymous callback function.
Type conversion
PHP is a weak language, and the variable type is determined by the context in which the variable is used.
(Full text)
The above introduces the type-PHP manual notes, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.