Home  >  Article  >  Backend Development  >  What are the data types in php? Introduction to php data types

What are the data types in php? Introduction to php data types

不言
不言forward
2018-10-23 17:09:344584browse

This article brings you what are the data types of PHP? The introduction of PHP data types has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP’s data types are divided into three major categories and eight subcategories:

1. Scalar type

boolean (Boolean): This is the simplest Type has only two values, which can be TRUE/true or FALSE/false, and are not case-sensitive. For details, please see: PHP Boolean type (boolean)

integer (integer): Integer values ​​can be expressed in decimal, hexadecimal or octal, and can be preceded by an optional symbol (- or ). Octal represents a number that must be preceded by 0 (zero), and hexadecimal represents a number that must be preceded by 0x. For details, please see: PHP integer data (integer)

float (floating point type, also called double): For details, please see: PHP floating point type (float)

string (string ): Unlike other programming languages, character variables are divided into characters and strings. In PHP, character variables are uniformly used to define characters or strings. For details, please see: PHP string type (string)

2, composite type

array (array): array type variable is a special variable type

object (Object): Object is also a special data type. To create an object variable, generally use the new keyword to obtain it. For details, please see: PHP Object Type (object)

3, Special Type

resource (resource type): Resource is a special variable that stores a reference to an external resource. Resources are created and used through specialized functions. For details, please see: PHP resource type (resource)

NULL (null): Indicates that a variable has no value. The only possible value of NULl is NULL

Note: PHP is a weakly typed language. Its variables have no data type, but the data stored in the variable has a corresponding data type

Integer data type: uses 8 bytes for storage, and provides a variety of integer storage methods

 $num = 10;           // 十进制
    $num1 = 0123;        // 八进制(83)
    $num2 = 0x123;      // 十六进制(291)
    $num3 = 0b1101;     //二进制(13)

Floating point data type: uses 8 bytes Storage, and provides a variety of storage methods

 $float_num = 1.23;
    $float_num1 = 1.23e3;       // 1.23乘10的3次方

The storage method in the memory is: take out part of the 8 bytes to store the index, and part to store the valid digits. At certain times, when it exceeds a certain range, resulting in loss of accuracy.

String data type

In PHP, all data entered by the user and data defined by the programmer using quotation marks (single quotation marks and double quotation marks) are understood by the system as strings, PHP7 The length of a string is theoretically unlimited

Both single quotes and double quotes can define strings, but they are different from each other:

--Only a small number of escapes can be parsed in single quotes Symbols: \', \

--Double quotes can parse more escape characters: $, \", \n

Double quotes can parse nested characters in strings PHP variables (variables should be separated from other strings, use {})

$a = "你好";
    $c = "$a世界";
    // $c = "{$a}世界" 常用
    echo $c;        //输出“你好世界”

String View

You can use the subscript $str[1] to view the subscripted characters in the string , when the index is a negative number, the index direction is from back to front

Define the array

Arrays can be defined in many ways in PHP. The common ways are as follows:

-- Use the array keyword to initialize the array

-- Use the array brackets [ ] to initialize the array

 // 使用array
    $arr1 = array();        // 可以不指定元素
    $arr2 = array("name", "age");
 // 使用[]定义数组
    $arr3 = [];
    $arr4 = ["name", "age"];

Characteristics of arrays

1. PHP array Theoretically there is no limit on the number of elements

2. Elements can be dynamically added to the PHP array

3. The value of the PHP array element can be any data type

4. PHP array The subscript can be a pure number (indexed array), a pure string (associative array), a mixed number and a string (mixed array)

Type judgment

Determine the data type through system functions: function The format starts with is_, followed by the corresponding data type, and the return result is a Boolean type.

Type conversion

Automatic conversion: PHP will automatically convert different data according to the operation scenario in which the data will participate. The data of the qualified data type is converted to the target type data. This conversion will not change the original data type of the variable.

Forced conversion: Use int (target type) format to convert the data

The above is the detailed content of What are the data types in php? Introduction to php data types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete