This article is forwarded from: blog.csdn.net/KH_FC/artic…
The following eight types of PHP data types are supported:
type | instructions |
---|---|
string | string |
boolean | Boolean value |
integer | The integer |
float | floating-point |
array | An array of |
object | object |
resource | resources |
NULL | NULL |
# 1. Basic types | |
Basic types are the most basic data types, which are used to represent simple and direct data. Basic types include the following data types: | |
type | instructions |
– | – |
Boolean (Boolean) | There are only two values, one istrue , one isfalse . Most are used to judge whether something is true or false |
String (string) | The value contains the set of all the characters that the computer can display, mostly for illustrative characters |
Integer (integer) | Contains only integers, including positive and negative integers |
Float (float) | Contains integers and decimals |
## 1.1 Boolean | |
The Boolean valuetrue andfalse These two values, therefore, are the simplest and most commonly used data types in PHP, and the two values of the Boolean value can be used directly, case insensitive, when setting variables can be directly assigned to the two values of the Boolean value can be used, usually with flow control. |
|
【Example 1Normally set the variable to a Boolean value | |
“`php | |
<? php |
$a = true; $b = false; \\ Sets the value of variable b to falseCopy the code
? >
[** example 2**] PHP <? php $a = true; $b = false; If ($a == $b) {echo "; }else{echo "value not equal "; # false output}? >Copy the code
The output is as follows: Values are not equal [Example 3] Returns a Boolean value using an operator
$name = "Zhang";
if ($name= ="Zhang") {echo "His name is$name"; Print if the result is true
}
? >
Copy the code
The output is as follows: His name is Zhang SAN
if ($a = true ){
var_dump($a);
}
? >
Copy the code
Bool (true); bool(true); var_dump(); bool(true);
if ($a) {
var_dump($a);
}
? >
Copy the code
$a = $a; $a = $a
1.2 the integer
Integer values contain only integers, which can also be used in octal, hexadecimal, and binary tables. Negative values can be preceded by optional – and + symbols. An octal integer that must be preceded by a 0 before being represented
$a = 0456;
var_dump($a);
? >
Copy the code
The output is: int(302), octal 456 converted to decimal is 302 [example 2] a hexadecimal integer that needs to be preceded by 0x
$a = 0x456;
var_dump($a);
? >
Copy the code
The output is as follows: int(1110), the hexadecimal 456 is 1110 when converted to decimal. [Example 3] The binary integer must be preceded by 0b
$a = 0b1101;
var_dump($a);
? >
Copy the code
The output is: int(13), the binary number 1101 is converted to decimal number 13 [Example 4] Print negative numbers, just add – before the positive number
$a = -100;
var_dump($a);
? >
Copy the code
Int (-100)
Generally, the maximum value of an integer is 2 billion characters, that is, 32 characters. The maximum value of an integer is 9E18 on a 64-bit platform. To check the maximum value and minimum value, run the PHP_INT_MAX and PHP_INT_MIN command
var_dump(PHP_INT_MAX);
? >
Copy the code
Int (9223372036854775807); int(9223372036854775807)
var_dump(PHP_INT_MIN);
? >
Copy the code
Int (-9223372036854775808) int(-9223372036854775808) int(-9223372036854775808)
$a = 01239;
var_dump($a);
? >
Copy the code
The following error message is displayed: 【Pay attention toAn integer value outside the integer range is interpreted as a floating-point number, as is the result of an operation outside the integer range. 【The sampleThe result of the operation is outside the integer range
$num1 = 45678901111;
$num2 = 89099899111;
var_dump($num1 * $num2);
? >
Copy the code
Float (4.0699854804914E+21)
1.3 floating-point
Floating-point integers are also called real numbers. They can be expressed in both normal and technical format
$a = 1.234;
var_dump($a);
? >
Copy the code
The output is float(1.234)
$a = 1.2 e3;
$b = 8E - 10;
var_dump($a);
var_dump($b);
? >
Copy the code
Float (1200) float(8.0E-10)
1.4 the string
A string is a series of characters, a character is a byte, defined by single quotes, double quotes, Heredoc syntax constructs, and Nowdoc syntax constructs.
- Single quotes
If you want to output a single quote, you need to add the escape character \\ to the single quote. If you want to output a backslash at the end, you need to add the escape character \\ before the character. If you add the escape character \ or \\ before other characters, you will directly output the escape character. [Example 1] Normally output a single-line character string
$a = 'I'm a string';
var_dump($a);
? >
Copy the code
The output is as follows: string(21) “I am a string” [Example 2] Normally the output is a multi-line string
$a = 'I'm a multi-line string';
var_dump($a);
? >
Copy the code
The output is: tring(23) “I am a multi-line string” [Example 3] Displays single quotes in a string
$a = 'I like \'PHP\'';
var_dump($a);
? >
Copy the code
The output is as follows: string(12) “I like ‘PHP'” [Example 4] Print backslashes in a string, usually used for file paths
$a = 'D:\\php\index.php';
var_dump($a);
? >
Copy the code
String (16) “D:\ PHP \index.php” string(16) “D:\ PHP \index.php” string(16) “D:\ PHP \index.php
$a = 'Joe';
$b = 'my name is $a';
$c = "My name is$a";
var_dump($b);
var_dump($c);
? >
Copy the code
$a = $a; $b = $a; $c = $a; Double quotation marks use double quotation marks to define strings. The character type to be defined is “”. When using double quotation marks to define strings, variables in the string will be interpreted without displaying their names. Escape character list:
Escape character | meaning | ASCLL code value (decimal) |
---|---|---|
\a | Ring the bell (BEL) | 007 |
\b | Backspace (BS) to move the current position to the previous column | 008 |
\f | Page feed (FF) moves the current position to the beginning of the next page | 012 |
\n | Line feed (LF) moves the current position to the beginning of the next line | 010 |
\r | Enter (CR) to move the current position to the beginning of the line | 013 |
\t | Horizontal Tabulating (HT)(Skip to next TAB position) | 009 |
\v | Vertical tabulating (TV) | 011 |
\ \ | Represents a backslash character “\” | 092 |
\ ‘ | Represents a single quotation mark (apostrophe) character | 039 |
\” | Represents a double quote character | 034 |
\? | For a hello | 063 |
\ 0 | NULL character (NULL) | 000 |
\ddd | Any character represented by an octal number of 1 to 3 digits | Three digit octal |
\xhh | Any character represented in hexadecimal with 1 to 2 digits | Two hexadecimal |
【Pay attention to】 to note\ and/ , is not replaceable |
||
【Example 1Output when double quotes define a string$ symbol |
||
“`php | ||
<? php |
echo "\$";
Copy the code
? >
[** Example 2**] double quotes PHP <? PHP echo "My Name is \" li Ming \"; ? >Copy the code
<<< define a string. When using a prompt to define a string, define an identifier after the prompt, then define a new line that contains the string you want to display, and end with the identifier. Identifier definitions must follow PHP rules and must contain only case, numbers, and underscores, but cannot start with underscores.
$str = <<;
? >
Copy the code
The output is: My name is “zhangsan” has been added to CSDN for a year. Defining a string using the Nowdoc syntax is similar to defining a string using Heredoc, except that identifiers are surrounded by single quotation marks. Variables in a string cannot be interpreted as characters.
$name = "Zhang";
$str= < < <'test'
My name is $nameMy name is Zhang SAN test;echo $str;
? >
Copy the code
The command output is: My name is $name My name is zhangsan
2. Compound types
Compound types include both array and object data types
type | instructions |
---|---|
Array (array) | An ordered combination of data |
The object (objects) | Objects are instances of classes that are created using new |
# # 2.1 array | |
An array is a key-value pair. Each array consists of an index (key name) and a value. The index of an array can be a string or a number, and the value can be any data type | |
【Example 1Use the function to create an array | |
“`php | |
<? php | |
$array = array( |
"Name" => "age" => "11",Copy the code
); var_dump($array); ? >
Output: ` ` ` array (2) {/ "name" = > string (6) "zhang SAN" (" age ") = > string (3) the "11"} ` ` ` 2 * * * * instance 】 【 direct assignment to create an array ` ` ` PHP <? $array = ["name" => "$array ", "age" => "11",]; var_dump($array); ? >Copy the code
Array (2) {[“name”]=> string(6) “age” [“age”]=> string(3) “11”
$array = array("Zhang"."Bill"."Fifty");
var_dump($array);
? >
Copy the code
The output is: Array (3) {[0]=> string(6) “three” [1]=> String (6) “four” [2]=> String (6) “five”} When the first one reads an array from zero, it can be read with the index name enclosed in square brackets
$array = array(
"name"= >"Zhang"."age"= >"11",); var_dump($array['name']);
? >
Copy the code
The output is: string(6) “triple”.
$array = array(
"name"= >"Zhang"."age"= >"11",);$array['name'] = "Bill";
var_dump($array);
? >
Copy the code
The output is: the array (2) {/ “name” = > string (6) “bill” (” age “) = > string (2) the “11”}
2.2 object
Objects are the basis of object-oriented programming, and are instantiated in PHP using new, that is, creating objects
class test{
function fu(){
echo "test"; }}$test = new test;
$test->fu();
? >
Copy the code
The command output is test
3. Special type
Special data types include resources and null values
type | instructions |
---|---|
The resource (resources) | This also refers to external resources, such as uploaded files, etc |
NULL (NULL) | Null when no variable is assigned |
# # 3.1 resources | |
Resource data type variables hold special handles to open files, database connections, graphics canvas areas, etc. It is not recommended to convert other data types to resource types, which are mostly used in file upload, download, database and other scenarios | |
Resource data type The resource reclamation mechanism is automatically enabled to release unused resources at any time. This prevents programs from occupying a large number of server resources | |
Record the use of resources for you later. | |
# # 3.2 NULL | |
NULL is a NULL value, which means that the variable is not assigned a value, and its value is a special valuenull , this value is case insensitive. Null values exist when the variable is null, the variable is not assigned, and is usedunset() The variable treated by the function |
|
【Example 1Set the value of the variable to null | |
“`php | |
<? php | |
$test = null; | |
var_dump($test); | |
? > | |
` ` ` | |
The output is:NULL |
|
【Example 2Prints the value of a variable when it is not set | |
“`php | |
<? php | |
var_dump($a); | |
? > | |
` ` ` | |
The output is:NULL |
|
【prompt】 useunset() The function will destroy the variable, usingis_null() The function determines whether a variable is null |
|
【Example 3Determines whether the variable is null | |
“`php | |
<? php | |
$a = $a; | |
if (is_null($a)){ |
Echo "variable null ";Copy the code
}else{echo “variable not empty “; }? >
The output is: the variable is not emptyCopy the code