Home > Backend Development > PHP Tutorial > How Do Nullable Types Work in PHP, and What Are the Syntax Rules?

How Do Nullable Types Work in PHP, and What Are the Syntax Rules?

Patricia Arquette
Release: 2024-12-09 15:14:10
Original
225 people have browsed it

How Do Nullable Types Work in PHP, and What Are the Syntax Rules?

Nullable Types in PHP 7: Understanding ? Before Type Declaration

Nullable types are a powerful feature introduced in PHP 7.1 that allow you to declare variables, parameters, and return values as nullable. This signifies that they can accept or return either the specified type or NULL.

Nullable Parameters

Using a question mark (?) before the type declaration in function parameters indicates that the parameter can be either the specified type or NULL. For example:

public function (?string $parameter1, string $parameter2) {}
Copy after login

In this example, both parameters are nullable. You can pass either a string value or NULL as an argument to the function.

Nullable Return Types

Similarly, a function's return type can be declared as nullable using the question mark syntax. This indicates that the function can return either the specified type or NULL. For example:

function error_func(): int {
    return null ; // Uncaught TypeError: Return value must be of the type integer
}

function valid_func(): ?int {
    return null ; // OK
}
Copy after login

Nullable Property Types (PHP 7.4 )

Starting with PHP 7.4, you can also declare property types as nullable. This allows for properties to have either the specified type or NULL. For example:

class Foo
{
    private ?object $bar = null; // OK : can be null (nullable type)
}
Copy after login

Nullable Union Types (PHP 8 )

PHP 8.0 introduced a shorthand notation for nullable union types. Writing "?T" is equivalent to "T|null". This allows for more concise type declarations.

class Foo
{
    private object|null $baz = null; // as of PHP 8.0
}
Copy after login

Syntax Error in PHP Versions Lower than 7.1

If you try to use the question mark syntax with PHP versions lower than 7.1, you will encounter a syntax error. In such cases, you should remove the question mark.

Conclusion

Nullable types provide a convenient way to handle nullable values in PHP. They allow you to declare that a variable, parameter, or return value can be either a specific type or NULL, making your code more robust and easier to maintain.

The above is the detailed content of How Do Nullable Types Work in PHP, and What Are the Syntax Rules?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template