Variables and Data Types

The key object of almost any programming languages is the variable. A variable is generally understood as a named memory area.

In this area, either a string, or a number, or a complex object can be stored. You can manipulate this value using the variable name (for simplicity, we will call it simply a variable). What is stored in the memory area will be called the variable value.

Variable declaration. Operator =

In PHP, variables begin with a dollar sign ($), followed by any number of alphanumeric characters and underscores, but the first character cannot be a number. Thus, the following variable names are valid: $n, $nl, $user _ func _ 5, etc. Unlike keywords, variable names in PHP are case-sensitive, i.e. the $user, $User and $USER variables are different (Listing 4.1).

Listing 4.1. The dependence of variables on the register. File case_sensitive.php

$user = "John";
$User = "Jack";
$USER = "Michael";
echo $user; // John
echo $User; // Jack
echo $USER; // Michael

As you can see from Listing 4.1, to assign a value to a variable, you must use the assignment operator =, which allows you to initialize the variable.

When declaring numeric values, the point appears as a separator for the integer value and the fractional part (Listing 4.2).

Listing 4.2. Declaring numbers. File number_set.php

$number = 1;
$var = 3.14;

Initialization by a single value of several variables is allowed due to the fact that the operator = returns the result of the assignment. In Listing 4.3, the variables $num, $number and $var are assigned the value 1 in one line by using the = operator in the chain.

Listing 4.3. Initializing variables with one value. multi_set.php file

$num = $number = $var = 1;

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.


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 typeDescription
integerAn 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
booleanA logical type that can take only two values: true and false
stringString type. Can store a string, the maximum size of which is 2 GB
arrayArray. 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.
objectAn object. This construction combines several different types of variables and methods for their processing.
resourceA 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.
nullA 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.

/ October, 15 at 19:25

I create web projects, develop, optimize and promote websites. If you have any ideas or want to suggest something, then write to me and my team.

Top ↑

Leave a Reply