02_ PHP data type


1. PHP data type

The data types of PHP are mainly divided into three categories:

  • scalar data type

    • String (string)
    • Integer (integer)
    • Float (floating point)
    • Boolean (Boolean)
  • Composite data type

    • Array
    • Object
  • Special data type

    • Resource
    • Null (null)

Scalar data type is the most basic unit in data structure. It can only store one data.

1.1. String (string)

A string is a sequence of characters consisting of consecutive letters, numbers, or characters.
PHP provides four ways to represent strings: single quotation marks, double quotation marks, heredoc syntax structure and nowdoc syntax structure (delimiter < <).

① The difference between single quotation marks and double quotation marks is as follows.

  • The variables in the “” double quotation marks will be parsed by the compiler before output.
  • The variables in ” single quotation marks are output directly without parsing, so the execution of single quotation marks is faster than that of double quotation marks.

For example:

$number= 100;
echo '$number=', $number; // The result is: $number = 100
echo "$number=", $number; // The result is: 100 = 100

: when defining a string, if there are no special requirements, try to use single quotation marks.

  • Use of escape characters

The special characters that can be output through the “\” escape character in double quotation marks are as follows:

Escape sequence character
\n Newline character
\r Carriage return
\t Tab
\\ Backslash
\$ Dollar sign
\” Double quotation mark

The only special characters that can be output through the “\” escape character in single quotation marks are:

Escape sequence character
\\ Backslash
\’ Single quotation mark

② Heredoc and nowdoc syntax structures (delimiter < < <)

The syntax structures of heredoc and nowdoc are:

Start with “< <“,
Followed by the start identifier name,
The identifier name referenced at the end should be the same as the start identifier name, ending with a semicolon.


The use of heredoc and nowdoc structures has the following characteristics:

  • The start and end tags use the same string, usually written in uppercase letters.
  • Spaces or extra characters cannot appear after the start tag.
  • The end tag must be written at the beginning without indents and spaces.
  • The variables in the heredoc structure will be resolved, and the variables do not need to be spliced with the connector “.”.

The syntax differences between heredoc and nowdoc structures are:

  • The name of the start identifier. Heredoc has no quotation marks. Nowdoc must be wrapped in single quotation marks.
  • The variables in the heredoc structure will be parsed, similar to double quotation marks; Variables in the nowdoc structure are output as is, similar to single quotes.
$name = 'PHP';
$heredoc = <<<EOD
<p>$name is the best language in the world</ p>
echo $heredoc;   // Output: PHP is the best language in the world!
$nowdoc = <<<'EOD'
<p>$name is the best language in the world</ p>
echo $nowdoc;  // Output: $name is the best language in the world!

See the string section for details of string operation

1.2. Integer

Integer data can only contain integers.

The rules for integer data are:

  • Integer data must have at least one number (0 ~ 9).
  • Integer data cannot contain commas or spaces.
  • Integer data has no decimal point.
  • Integer data can be positive or negative.

Integer data can be specified in three formats,
That is, decimal, hexadecimal (prefix 0x) and octal (prefix 0).

$oct = 012;  // octal number system
$dec = 10;   // decimal system
$hex = 0xa;  // hexadecimal

be careful, in PHP 7, strings containing hexadecimal characters are no longer treated as numbers, but as ordinary strings.

1.3. Float

Floating point data can be used to store not only integers, but also decimals and exponents.

$num = 3.14;

1.4. Boolean

There are only two Boolean data, true and false. Boolean variables are usually used in conditional judgment statements.

Other data types basically have Boolean attributes:

data type true false
integer Not 0 0
float Not 0 0.0
String type Nonnull nonzero ” empty string or ‘0’ string
Array type Include elements Does not contain any elements
Object type Always true
Resource type Always true
Null type Always false

1.5. Array

Array is an operable whole formed by a series of data. A series of data can include scalar data, objects, resources and other syntax structures supported in PHP.

Generally, a single data in an array is called an element, and the element is divided into two parts: index (key name) and value.
The index (key name) can be a number or a string, and the value can be any data type.

In PHP, array () is usually used to create arrays or assign values directly. To be exact, array () is a structure rather than a function. The specific format is as follows:

$array1 = array('val1','val2','val3'...);
$array2 = array(key1 => 'val1', key2 => 'val2', key3 => 'val3',...);
$array[key1] = 'val1';

See the array section for details

1.6. Object

Programming languages are applied to two methods: process oriented and object-oriented. In PHP, users are free to use both methods.

Object data types can also be used to store data. In PHP, objects must be declared. First, you must declare class objects using the class keyword. A class is a structure that can contain properties and methods. Then define the data type in the class and use the data type in the instantiated class.

1.7. Resource

Resource is a special variable type, also known as handle, which is created and used through special functions. Resources can be operated, such as creation, use, release, etc. let

Any resources should be released in time when they are not needed. The system will also automatically enable the garbage collection mechanism to recycle resources after the page is executed to avoid memory depletion.

In the process of programming, resource types are almost always inaccessible.

1.8. Null (null)

Null value, as the name suggests, means that no value is set for the variable.

There are three situations in which null values are assigned:

  • No value has been assigned
  • Assigned null
  • Variable processed by unset() function.

1.9. Data type detection

PHP has built-in series of functions to detect data types, which can detect different types of data to determine whether they belong to a certain type. If they meet the requirements, they return true, otherwise they return false.

The functions for detecting data types are shown in the following table:

function Detection type give an example
is_bool() Boolean type is_bool(srue) , is_bool(false)
is_string() Is it string type is_string(‘string’) , is_string(1234)
is_float()/is_double() Floating point is_float(3.1415) , is_float(‘3.1415’)
is_integer()/is_int() Is it an integer is_integer(34) , is_integer(‘34’)
is_null() Is it null is_null(null)
is_array() Is it an array is_array($arr)
is_object() Is it an object is_object($obj)
is_numeric() Is it a number or a string of numbers is_numeric(‘5’) , is_numeric(‘bcc110’)
    $boo = '0123456';
        echo 'yes!';
        echo 'no!';    

1.10. Data type acquisition

1. If you want to view the value and type of an expression, use var_ dump()。
2. If you just want to get an easy to read expression of type for debugging, use GetType ().
3. To view a type, use is instead of gettype()_ Type() function.

1.11. Data type conversion

Although PHP is a weakly typed language, it sometimes needs type conversion. There are two main conversion methods for data types in PHP.

1.11.1. Automatic type conversion

The so-called automatic type conversion means that when the operation needs or does not match the expected result type, PHP will automatically carry out type conversion without any operation by developers.

In the process of program development, there are three most common automatic type conversion, namely, conversion to Boolean, conversion to integer and conversion to string. The following points should be paid attention to when automatically converting various types.

  • When converted to Boolean value, null, integer value 0, floating point value 0.0, empty string and string “0” will be turned to false, and other values will be turned to true.
  • When Boolean is converted to integer, Boolean value true is converted to integer 1; The Boolean value false is converted to an integer 0.
  • When converting a floating-point number to an integer, the decimal part is removed and rounded down.
  • When converting a string type to an integer type, if the string starts with a number, the value is used; otherwise, it is converted to 0.
  • When Boolean is converted to string, the Boolean value true is converted to string “1”; The Boolean value false is converted to an empty string.
  • When converting an integer or floating-point type to a string, the number is directly converted to string form.

1.11.2. Cast type

The so-called forced type conversion is to manually convert data types when writing programs. There are three main conversion methods.

1) Precede the variable to be converted with the target type enclosed in parentheses. This method only needs to add the type name enclosed in parentheses in front of the variable. The types allowed to be converted are shown in the table:

function Detection type give an example
(int),(integer) Convert to integer (int)$str,(integer)$boo
(bool),(boolean) Convert to Boolean (bool)$num,(boolean)$str
(float),(double),(real) Convert to floating point (float)$str
(string) Convert to characters (string)$boo
(array) Convert to array (array)$str
(object) Convert to object (object)$str

2) Use three specific types of conversion functions, intval(), floatval(), strval(). Among them, intval() function is used to convert to integer, floatval() function is used to convert to floating point, and strval() function is used to convert to character.

3) Use the function settype() to convert. The settype() function is used to set the data type of the variable. It can convert the specified variable to the specified data type.

bool settype (mixed $var,string type)