# Nullable Types

> Section in “New and Extended Functionality › Type System” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/type-system/nullable-types/>

In the example shown below, the `int $x = null` type declaration means that parameter `$x` is of type `int`, nullable, optional, and has the default value `null`. When the parameter is omitted, then its value will be `null` inside the function’s body. When it is not omitted, then its value must either be of type `int` or `null`.

``` php
function f(int $x = null)
{
    var_dump($x);
}

f();
f(null);
f(1);
f('');
```

Executing the code shown above will print the output shown below:

    NULL
    NULL
    int(1)

    Fatal error: Uncaught TypeError:
    Argument 1 passed to f() must be of the type integer, string given

It is possible to declare parameters to be nullable but not optional. To do this, simply prefix a type declaration with a question mark:

``` php
function f(?int $x)
{
    var_dump($x);
}

f(null);
f(1);
f();
```

Executing the code shown above will print the output shown below:

    NULL
    int(1)

    Fatal error: Uncaught TypeError:
    Argument 1 passed to f() must be of the type integer, none given

For compliance with the Liskov Substitution Principle, the nullability of a parameter should not be removed in a child class:

``` php
class ParentClass
{
    public function m(?int $x)
    {
    }
}

class ChildClass extends ParentClass
{
    public function m(int $x)
    {
    }
}
```

Executing the code shown above will print the output shown below:

    Warning: Declaration of ChildClass::m(int $x) should be compatible
    with ParentClass::m(?int $x) ...

A child class should not accept less input values than the parent class. In this example, when passing `null` for parameter `$x`, you cannot substitute `ChildClass` for `ParentClass`:

``` php
$o = new ChildClass;
$o->m(null);
```

Passing `null` yields a type error at runtime:

    Warning: Declaration of ChildClass::m(int $x) should be compatible
    with ParentClass::m(?int $x) ...

    Fatal error: Uncaught TypeError: Argument 1 passed to ChildClass::m()
    must be of the type integer, null given, called in ...

Nullability of a parameter can be added in a child class, though, when the parameter’s type declaration is not nullable in the parent class. This means that a child class accepts more input values than the parent class.

---

Previous: [Scalar Type Declarations](https://php7explained.com/new-and-extended-functionality/type-system/scalar-type-declarations/index.html.md) | Next: [Return Type Declarations](https://php7explained.com/new-and-extended-functionality/type-system/return-type-declarations/index.html.md)
