There are three data types in PHP: scalar data type, composite data type, and special data type
PHP is a weak data type language. A variable in PHP can store any type of data. The variable type is determined by the data.
Strong data type language:
The characteristic of strong data type language is that before using a variable, the variable must be declared as a variable of this type according to the type of data the variable will store.
Java example:
Define a variable to store integer data
int v1 = 100
v1 = 'one' //Report an error immediately
PHP weak data type language:
Features, variables can store any data type.
php example:
Define a variable to store integer data
$v1 = 100;
$v1 = 'one';
The value of the integer type is a value in the set (...-2,-1,0,1,2,3....).
The integer occupies 4 bytes (4 means not 4 bits in decimal, but 32 bits in binary)
The maximum representation range of integer type: 2147483647
Principle:
(01111111111111111111111111111111)2=(2147483647)10
Integer values can also be octal or hexadecimal
Octal: starts with 0, the number cannot be greater than 7
Hexadecimal: starting with 0x, the numbers include 0 to 9, A (10), B, C, D, E, F (15)
Example:
You can use the printf() function to format the output data
Syntax:
printf('format string', data list)
Description:
The format string can contain some placeholders:
%d Decimal
%b Binary
%o Octal
%x Hexadecimal
Example 1:
Example 2:
Numbers with decimal points are decimal types (also called precision types). PHP divides decimal types into single precision (floatsingle) and double precision (double).
float type
double type
|
Bytes | Min. Value | Maximum value | ||||||||||||
Float | 4 span> | -3.402823466E 38 | 3.402823466E 38 | ||||||||||||
Double | 8 | -1.7976931348623157E 308 | 1.7976931348623157E 308 |
Integer type 4 bytes (32 binary digits) Maximum representation 2147483647
float 4 bytes (32 binary bits) 3.402823466E 38
IEEE organizational regulations (Institude Electrical Electroncity Engineer)
Constraints on floating point numbers:
0 000 0000 0 000 0000 0000 0000 0000 0000
0sign sign bit 1 bit 0 means positive number 1 means negative number
000 0000 0 The exponent index bit has 8 digits
000 0000 0000 0000 0000 0000Valid data bits Mantisa 23 bits
If the exponent bit and valid data bit are both 0, it means the data is 0, as follows:
000 0000 0 000 0000 0000 0000 0000 0000
The exponent bits are all 1, and the valid data bits are all 0, which means the data is infinite, as follows:
111 1111 1 000 0000 0000 0000 0000 0000
The exponent bits are all 1, and the valid data bits are all non-zero, which means the data is null, as follows:
111 1111 1 000 0000 0000 0000 0000 0000
The data range represented by
is determined by the exponent bit and the valid data bit.
Exponent bit:
11111110 254 We need to subtract 127 127
127th way of 2 Decimal number => 1.7014118346046923e 38
Valid data bits:
There is a hidden 1 before the valid data bit
000 0000 0000 0000 0000 0000
1.11111111111111111111111 Decimal number =>1.99999999999 (approximately equal to 2)
The exponent bits are multiplied by the valid data bits:
1.7014118346046923e 38*2 is approximately equal to 3.402823466E 38
Example:
Mainly used to express the only two states of things, right, wrong, true and false
Value:
true means established
false means not true
The Boolean type value itself has no meaning and is mainly used for process control.
A collection of 0 or more characters
character definition method:
Method 1:
A string defined using single quotes
Method 2: String defined using double quotes
Method 3: Delimited string
Syntax:
$v1 = << Content
end of delimiter;
Example:
Note:
The start tag and end tag of the delimiter must be consistent
There can be no spaces before the closing tag
The delimiter can be understood as an html editor.
Delimiters can be divided into two categories: heredoc and nowdoc
The syntax of heredoc:
Syntax:
$v1 = << Content
end of delimiter;
nowdoc syntax:
Syntax:
$v1 = <<<'Delimiter starts'
Content
end of delimiter;
Example:
Note:
The use of single quotes and double quotes. Generally, if there are no variables in the string, use single quotes (the efficiency will be higher).
array(array)
Arrays are mainly used for storage, multiple data (variables) with certain relationships
Example:
object(object)PHP Advanced
The resource type also measures a special variable. Programmers cannot perform any operations on the resource type and can only view (var_dump).
Resource types can only be created through special functions provided by PHP.
The resource type is mainly used to represent PHP’s extended resources (data outside of PHP), such as: a data connection, a result set of a data table, an external text file, etc.
Null is also a special variable in PHP, which means null. Generally, you can check an undefined variable, or the variable has been unset().
Composite data type
Special data data types:
resource (resource type)
null(empty)