Exploring New Data Types

Learn about new data types that are introduced in PHP 8 and how they improve code quality.

One thing any entry-level PHP developer learns is which data types PHP has available and how to use them. The basic data types include int (integer), float, bool (boolean), and string. Complex data types include array and object. In addition, there are other data types, such as NULL and resource. In this lesson, we discuss a few new data types introduced in PHP 8, including union types and mixed types.

Note: It’s extremely important not to confuse a data type with a data format. This section describes data types. A data format, on the other hand, would be a way of representing data used as part of a transmission or for storage. Examples of a data format would include XML, JavaScript Object Notation (JSON), and YAML Ain’t Markup Language (YAML).

Union types

Unlike other data types, such as int or string, it’s important to note that there is no data type explicitly called union. Rather, when we see a reference to union types, what is meant is that PHP 8 introduces a new syntax that allows us to specify a union of types instead of just one. Let’s now have a look at the generic syntax for union types.

Union type syntax

The generic syntax for union types is as follows:

Get hands-on with 1400+ tech skills courses.