I found out a php file inside which it had a function like below:
public function getCharset(): ?string
{
return $this->charset;
}
I want to know what is : ?string doing here.
I found out a php file inside which it had a function like below:
public function getCharset(): ?string
{
return $this->charset;
}
I want to know what is : ?string doing here.
This is known as a nullable type, and is introduced in PHP 7.1:
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type,
NULL
can be passed as an argument, or returned as a value, respectively.
Essentially, the function can return either the specified type or null
. If it would return a different type, an error is thrown:
function answer(): ?int {
return null; // ok
}
function answer(): ?int {
return 42; // ok
}
function answer(): ?int {
return new stdclass(); // error
}