Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

PHP type declarations — A guide for beginners

PHP 7 introduced a lot of new features to the language. One of them is the ability to declare the type of a function argument and the type of value that a function returns. This is called type declaration.

In this article, we’ll learn about PHP type declarations and how to use them.

What are PHP type declarations?

Type declarations are a way to tell the PHP interpreter that a function argument should be of a certain type and that the function should return a value of a certain type.

For example, if you want to declare a function that accepts only an integer as an argument, you can do so by specifying the type of the argument as int as follows.

function add(int $a, int $b)
{
    return $a + $b;
}

echo add(1, 2); // 3

Similarly, you can also declare the return type of a function. For example, if you want to declare a function that returns only an integer, you can do so by specifying the return type as int as follows.

function savePayment(int $amount): int
{
    // do something

    return $amount;
}

List of types supported by PHP

PHP supports the following types of type declarations.

  • int - An integer
  • float - A float
  • string - A string
  • bool - A boolean
  • array - An array
  • callable - A callable
  • iterable - An iterable
  • object - An object
  • self - The class itself
  • parent - The parent class
  • ClassName - A class name
  • interfaceName - An interface name
  • static - The late static binding type

Apart from these, you can also use the mixed type from PHP 8 onwards to indicate that the function accepts any type of argument and the void type to indicate that the function does not return anything.

There are also union types which allow you to specify multiple types for a function argument or return type. For example, if you want to declare a function that accepts either an integer or a float as an argument, you can do so by specifying the type as int|float as follows.

function add(int|float $a, int|float $b): int|float
{
    return $a + $b;
}

echo add(1, 2); // 3
echo add(1.5, 2.5); // 4

On the contrary to union types, there are also intersection types which allow you to specify that the function argument or return type should be an instance of multiple types. For example, if you want to declare a function that accepts an argument that is both an integer and a float, you can do so by specifying the type as int&float as follows.

function add(int&float $a, int&float $b): int&float
{
    return $a + $b;
}

echo add(1, 2); // 3
echo add(1.5, 2.5); // 4

PHP also supports nullable types which allows you to specify that the function argument or return type can be null as well. For example, if you want to declare a function that accepts an integer or null as an argument, you can do so by specifying the type as ?int as follows.

function addUserName(?int $id, ?string $name): ?string
{
    return $name;
}

echo addUserName(1, 'John'); // John
echo addUserName(1, null); // null

PHP 8.1 also introduced the never type which is used to indicate that the function never returns a value. This is useful when you want to throw an exception from a function. For example, if you want to declare a function that throws an exception, you can do so by specifying the return type as never as follows.

You can also use the never type to indicate that the function will exit the script. Here’s an example that depicts this.

function add(int $a, int $b): never
{
    if ($a === 0 || $b === 0) {
        throw new Exception('Invalid argument');
    }

    $sum = $a + $b;
    exit();
}

Strict typing

By default, PHP is not strict about the types of arguments that a function accepts and the type of value that a function returns. This means that if you pass a float value to a function that accepts only integers as arguments, PHP will automatically convert the float value to an integer and pass it to the function.

Similarly, if you return a float value from a function that is declared to return only integers, PHP will automatically convert the float value to an integer and return it.

To mitigate this, PHP 7 introduced a new feature called strict typing. This feature allows you to enable strict typing in your code. This means that if you try to pass an argument of a different type to a function that expects a certain type, it will throw a fatal error.

For example, if you try to pass a float value to the add() function that expects an integer, it will throw a fatal error.

declare(strict_types=1);

function add(int $a, int $b): int
{
    return $a + $b;
}

echo add(1.1, 2.1);
// Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, float given

Now, if the strict typing is disabled, it will not throw a fatal error and will try to infer the float value to an integer. So, the following will work just fine.

function add(int $a, int $b): int
{
    return $a + $b;
}

echo add(1.1, 2.1); // 3

Why use PHP type declarations?

Type declarations are a great way to enforce strict typing in your code. This means that you can be sure that the function will always receive the type of argument that you expect and that the function will always return the type of value that you expect.

This is especially useful when you’re working with a team of developers and you want to make sure that the code is consistent and that the code is not broken by someone else. It’s a safety net that gives you peace of mind that the code is working as expected no matter who is working on it.

In closing

In this article, we learned about PHP type declarations and how to use them. We also learned about the different types supported by PHP and how to use them. We also learned about strict typing and how to enable it in your code.

I hope this article helped you learn about PHP type declarations. If you have any questions, feel free to ask them in the comments section below.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?