Home > Article > Backend Development > There are several variable types in php
There are 8 types of variables in php, namely: 1. 4 scalar data types (boolean, string, integer, float); 2. 2 composite data types (Array and Object); 3. 2 special data types (NULL and resource data types).
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP data types can be divided into three major categories , which are scalar data types, composite data types and special data types respectively. Let’s introduce these data types in detail below.
Scalar data type
The scalar data type is the most basic unit of the data structure and can only store one piece of data. There are four types of scalar data types in PHP, as shown in the following table:
Type | Function |
---|---|
boolean (Boolean) | The simplest data type, only two values: true (true) / false (false) |
string (string) | String is a continuous sequence of characters |
integer (integer type) | Integer type contains all integers, which can be positive or negative numbers |
float (floating point type) | Floating point type is also used to represent numbers. Unlike integer type, in addition to integers, it can also be used to represent decimals. and index |
String
A string is a continuous sequence of characters. In other languages, characters and strings are two different data types, but in PHP, characters are unified and string as string data type. In PHP, there are three ways to define strings, namely single quotation mark method, double quotation mark method, and Heredoc method.
The sample code is as follows:
<?php //双引号方式声明字符串 $str1 = "php中文网"; //单引号方式声明字符串 $str2 = 'PHP 教程'; //Heredoc 方式声明字符串 $str3 = <<<EOF url: //m.sbmmt.com/ EOF; echo $str1."<br>".$str2."<br>".$str3; ?>
The running result is as follows:
php中文网 PHP 教程 //m.sbmmt.com/
Integer type
In PHP, the integer variable is called It is of type integer or int, used to represent an integer. The rules of integer type are as follows:
The integer type must have at least one number (0~9);
The integer type cannot contain commas or spaces;
The integer type cannot contain the decimal point;
The integer type can be a positive number or negative number.
The value range of the integer type must be between -2E31 and 2E31, and can be expressed in three formats, namely decimal, hexadecimal (prefixed with 0x) and Octal (prefixed with 0).
The following uses an example to demonstrate the use of integers. The example uses PHP's var_dump() function, which can return the data type and value of the variable.
<?php $x = 5985; // 定义一个整型数据类型的变量 var_dump($x); // 输出此变量 echo "<br>"; $x = -345; var_dump($x); echo "<br>"; $x = 0x8C; //十六进制数字 var_dump($x); echo "<br>"; $x = 047; //八进制数字 var_dump($x); ?>
Run the above code, the results are as follows:
int(5985) int(-345) int(140) int(39)
Note that in the PHP7 version, strings containing hexadecimal characters are no longer treated as numbers, but as ordinary string, for example:
<?php var_dump("0x123" == "291"); echo "<br/>"; var_dump(is_numeric("0x123")); echo "<br/>"; var_dump("0xe" + "0x1"); ?>
Run the above code, the output result in PHP5 is as follows:
bool(true) bool(true) int(15)
The output result in PHP7 is as follows:
bool(false) bool(false) int(0)
Floating point type
Floating point type is called float type in PHP, also known as real number. It can be used to store integers and decimals. The valid value range is 1.8E-308 to 1.8E 308 between. Floating point numbers have higher precision than integer data types.
The sample code is as follows:
<?php $num1 = 10.365; $num2 = 2.4e3; $num3 = 8E-5; var_dump($num1, $num2, $num3); ?>
The running results are as follows:
float(10.365) float(2400) float(8.0E-5)
Boolean type
The Boolean type has only two values, respectively are TRUE and FALSE (case-insensitive), meaning logical true and logical false. The sample code is as follows:
<?php $x = True; $y = faLsE; var_dump($x, $y); ?>
The running results are as follows:
bool(true) bool(false)
Composite data type
The composite data type allows multiple Data of the same type is aggregated together and represented as an entity item. Composite data types include arrays (Array) and objects (Object) .
Array
An array is a collection of data, which is a whole formed by organizing the data according to certain rules. The essence of an array is to store, manage and operate a set of variables. According to the dimensions of the array, it can be divided into one-dimensional array, two-dimensional array and multi-dimensional array. We can create arrays using the array() function.
The sample code is as follows:
<?php $arr = array('website' => 'php中文网', 'url' => '//m.sbmmt.com/'); echo "<pre class="brush:php;toolbar:false">"; // <pre class="brush:php;toolbar:false"> 是一个 HTML 标签,用来格式化输出内容 var_dump($arr); ?>
The running results are as follows:
array(2) { ["website"]=> string(16) "php中文网" ["url"]=> string(23) "//m.sbmmt.com/" }
There are many applications of arrays. Here is just a brief introduction. We will provide them in subsequent studies. Please introduce in detail.
Object
Object (Object) can be used to store data. Objects must be declared in PHP. The class object must first be declared using the class keyword. Classes are structures that can contain properties and methods. Then define the data type in the class and use the data type in the instantiated class.
In a language that supports object-oriented, the common characteristics and behaviors of each specific thing can be abstracted into an entity, called a "class", and the object is the result of the class being instantiated using the new keyword. .
The sample code is as follows:
<?php class Car //使用 class 声明一个类对象 { var $color; function car($color="black") { $this->color = $color; } function getColor() { return $this->color; } } $car = new Car(); $car->car('red'); echo $car->getColor(); ?>
The running results are as follows:
red
We will explain more about object-oriented knowledge later.
Special data types
In PHP, there are data types used to specifically provide services or data, which do not belong to the above standard data types. Any type of data, so it is also called a special data type, mainly including NULL and resource data types.
#NULL
NULL is a special data type in PHP. It has only one value, NULL, which means a null value (the variable has no value). Please note that What's wrong is that it has a different meaning than spaces.
When the following conditions are met, the value of the variable is NULL:
The variable is assigned a NULL value;
Variable Before being assigned a value, the default value is NULL;
After using the unset() function to delete a variable, the variable value is also NULL.
NULL can usually be used to clear a variable. The sample code is as follows:
<?php $str = 'hello'; $str = NULL; var_dump($str); ?>
The running results are as follows:
NULL
Resources
Resource is also a special data type in PHP. It mainly describes a PHP extended resource, such as a database query (Query), an open file handle (fopen) or a database connection (Database Connection), and character stream (stream) and other extended types.
But we cannot directly operate this variable type and can only use it through special functions.
If one of the above situations occurs, for example, when using the fopen function to open a local file, the sample code is as follows
<?php header("content-type:text/html;charset=utf-8");//设置编码,解决中文乱码 $file = fopen("test.txt", "rw");//打开test.txt文件 var_dump($file); ?>
The running results are as follows:
resource(3) of type (stream)
资源是 PHP 提供的较强特性之一,它可以在 PHP 脚本中做自定义的扩展,类似于C语言结构中的引用,它的所有属性都是私有的,大家可以暂时将其理解为面向对象中的一个实例化对象。有关资源类型我们后面还会详细介绍。
推荐学习:《PHP视频教程》
The above is the detailed content of There are several variable types in php. For more information, please follow other related articles on the PHP Chinese website!