PHP 8 Union types Last Updated : 27 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report A “union type” accepts values of multiple different data types, rather than a single one. If the programming language supports union types, you can declare a variable in multiple types. For example, there can be a function that can accept the variable of type “string” or “float” as a parameter. PHP already supports two special union types. Type or null, using the special "?Type" syntax.Array or Traversable, using the special iterable type. But before the update, arbitrary union types were not supported by the language. Instead, we used PHPDoc annotations which was quite a work to do. Example 1: PHP <?php class GFG { /** * @var int|float $CodingScore */ private $CodingScore; /** * @param int|float $CodingScore */ public function setScore($CodingScore) { $this->CodingScore = $CodingScore; } /** * @return int|float */ public function getScore() { return $this->CodingScore; } } $a = new GFG(); $a->setScore(120.5); echo $a->getScore(), "\r\n" ; $b = new GFG(); $b->setScore(100); echo $b->getScore(); ?> Output: 120.5 100 But after this update, Union types are specified using the following syntax T1|T2|... It can be used in all positions where types are currently accepted as follows. Example 2: PHP <?php class GFG { private int|float $CodingScore; // Union type public function setScore(int|float $CodingScore): void { $this->CodingScore = $CodingScore; } //Union type public function getScore(): int|float { return $this->CodingScore; } } $a = new GFG(); $a->setScore(120.8); echo $a->getScore(),"\r\n"; $a->setScore(100); echo $a->getScore(); ?> Output: 120.8 100 Comment More infoAdvertise with us Next Article PHP 8 Union types A atul07 Follow Improve Article Tags : Web Technologies PHP PHP-basics Similar Reads TypeScript Union The TypeScript union has the ability to combine one or two different types of data (i.e., number, string, float, double, etc). It is the most powerful way to express a variable with multiple types. Use pipe ('|') symbol to combine two or more data types to achieve Union type. Syntax: (type1|type2|ty 3 min read PHP Data Types In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you donât need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable, 4 min read PHP Versions PHP has been a key technology in web development from the beginning, helping create interactive websites and web applications. Over the years, PHP has evolved, introducing new features, performance improvements, and better security. Understanding the different PHP versions and their changes is essen 3 min read TypeScript Defining a Union Type In this article, we are going to learn about Defining a Union Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a union type allows a variable to have one of several possible types. You can define a union type by using 3 min read PHP Type Juggling Type juggling in PHP refers to PHP's automatic conversion of values between data types during operations, such as arithmetic or comparison. This is also known as implicit casting and happens automatically during runtime.PHP automatically changes data types during operations like addition or comparis 3 min read Like