Variables, Data Types, and Type Casting in PHP

PHP is a dynamically typed language, which means that when you declare a variable, you don’t need to specify its type in advance. PHP determines the data type of a variable at the moment it is used, based on the value assigned to it.

Basic Data Types in PHP

There are 10 basic data types in PHP, which can be divided into categories:

Scalar types:

  • Boolean (bool) — takes the values true or false, often used in conditions.
  • Integer (int) — represents ordinary integers (positive and negative).
  • Floating point numbers (float) — used to store fractional numbers.
  • String (string) — used to store text.

Complex and special types:

  • Array — a data structure capable of storing multiple values under one name, accessed by keys.
  • Object — used to represent class instances in object-oriented programming.
  • Callable — a type representing a function or method passed as a value.
  • Resource — a special type representing external connections, such as to a database or file.
  • Null — means that a variable has no value.
  • Mixed — denotes a variable that can take values of any type.

Integers (int)

These are ordinary signed integers. In PHP, you can use not only the decimal (base 10) number system, but also others: binary, octal, and hexadecimal. Different forms of notation are interpreted by PHP as the same number, just with different syntax.

An integer in PHP usually occupies 32 bits of memory. This gives a range from -2,147,483,648 to 2,147,483,647. If it exceeds this limit, PHP automatically converts the value to a float type.

Floating point numbers (float)

These are used when you need to store numbers with a decimal point. This can be either a fixed value (e.g., 1.5) or an exponential notation (e.g., 1.3E4). The precision of floating point numbers in PHP is usually around 14 decimal places, but this can vary slightly depending on the platform.

Boolean type (bool)

Boolean variables can only take two values: true and false. They are most often used in conditional statements to control code execution. For example, they determine whether a particular block of code will be executed.

Strings (string)

Strings are sequences of characters. In PHP, strings can be enclosed in either double or single quotes. This is not just a cosmetic difference:

  • In double quotes, the interpreter substitutes variable values into the string text.
  • In single quotes, variables remain unchanged, like regular text.
  • You can also use escaping in strings to correctly display special characters, such as quotation marks, newline characters, etc.

Null: absence of value

The null type indicates that a variable does not contain any value. This can be useful when a variable has already been defined but not yet initialized, or to explicitly “clear” a variable. The null value is not case-sensitive, meaning you can write it as null or NULL.

Dynamic typing in PHP

One of the features of PHP is the ability to change the type of a variable during script execution. You can first assign an integer to a variable and then a string, and this will not cause any errors. The type of the variable will be determined automatically based on the last assigned value.

Conclusion

  • PHP is a flexible language with dynamic typing.
  • There is no need to declare variable types in advance — PHP determines them itself.
  • Developers have 10 different data types at their disposal, including both simple ones (such as numbers and strings) and more complex ones (arrays, objects, resources).
  • The type of a value can be converted by PHP automatically if necessary — this is called automatic type conversion.