4.2. Data types

The PHP language is weakly typed, in most cases, the variables of the language do not require strict specification of the type when they are declared, and during the execution of the program, the type of the variable can almost always be implicitly changed without special transformations.

For example, a variable declared by a string can be used further in arithmetic operations, act as a logical variable, and at the end an object can be assigned to it as a value. All this allows the developer to practically not think about data types.

However, some types, such as null, object, array, or resource, are so specific that successful development is impossible without considering their features.

Moreover, when using functions and methods, you can explicitly specify the type of their parameters and return values, which will be discussed in more detail in Chapter 11. In tab. 4.1 presents data types that are supported by PHP.

Comment

In the documentation and in our book, in addition to the data types presented in Table. 4.1, you will see pseudotypes – symbols of one or several types. They are mainly used in conjunction with functions and methods (see chapters 11 and 16). For example, <code>mixed</code> is any type, number is either integer or double, iterable is an iterable object. The key word void stands for the absence of a variable. Sometimes several types can be separated by a vertical bar | to indicate the fact that a variable can take one of the specified types. For example, the pseudotype number could be written using the following combination of base types: integer | double.

Table 4.1. PHP Data Types

Data type Description
integer An integer whose maximum value depends on the bitness of the operating system. In the case of a 32-bit operating system, the number can range from -2,147,483,648 to 2,147,483,647. If the bit depth is 64 bits, the range of possible values is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
double (or float) The real number, the minimum possible value of which is from ±2.23х10-308 to ± 1.79х1О308
boolean A logical type that can take only two values: true and false
string String type. Can store a string, the maximum size of which is 2 GB
array Array. This is a union of several variables under one name. You can access individual variables using the array index. Arrays are discussed in more detail in Chapter 10.
object An object. This construction combines several different types of variables and methods for their processing.
resource A descriptor that allows you to operate with a particular resource that is accessed using library functions. Descriptors are used when working with files, databases, dynamic images, etc. The descriptors will be discussed in more detail in the corresponding chapters.
null A special type that signals that a variable has not been initialized.
callable (or callback) Some PHP functions can take other functions as arguments, which are called callback functions. Variables of this type contain references to such functions.

In the current chapter, we will study only integers, real numbers, the logical type, strings, and the null type. The remaining types will be discussed in the following chapters.