What does question mark (?) before type declaration means in php (?int)

What does question mark (?) before type declaration means in php (?int)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Semantics of Nullable Types in PHP

You will often encounter type declarations prefixed with a question mark, such as ?int, particularly within modern codebases like the Symfony framework. In the constructor signature for Output, you see $verbosity defined as ?int. This syntax explicitly indicates that the parameter accepts either an integer value or null. Without this modifier, PHP would treat the absence of a value as a violation of the strict type hint, potentially triggering a fatal error depending on the configuration and context.

This operator was introduced in PHP 8.0 to provide a concise way to handle optional parameters while maintaining strong typing. Prior to version 8.0, developers typically relied on null coalescing operators or default values of null within the function body to achieve similar results, but the type system itself could not distinguish between "an integer is required" and "an integer or null is acceptable."

Consider a scenario where you are building a service that accepts a configuration value. If you define a method like this:

function process(int $value) {
    // Logic here
}

Passing null to this function results in a TypeError. However, if the requirement allows for an unset state or a fallback mechanism where null signifies "use default," you must explicitly allow it. The syntax evolves to:

function process(?int $value) {
    // Logic here
}

This distinction is crucial because it changes how PHP's static analysis tools, such as Psalm or PHPStan, interpret your codebase. These tools can now accurately determine when a function expects a value and when it accepts an absence of one. This improves developer experience by surfacing potential logic errors before runtime, rather than relying solely on exception handling to catch missing arguments.

In the Symfony example provided, the implementation handles this nullable type immediately inside the constructor body:

$this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;

Here, the logic treats null as a sentinel value that defaults to VERBOSITY_NORMAL. This pattern is common in API design where a lack of input should not crash the application but rather resolve to a sensible default. The question mark ensures the type checker does not complain when you pass null, while still enforcing that if a number is passed, it must be an integer.

A potential pitfall arises when mixing this with other nullable types like strings or arrays. While PHP allows ?string or ?array, care must be taken not to confuse ?string (a string that might be null) with string|null. Although they are technically identical in runtime behavior, the latter syntax is becoming more popular in newer versions of PHP (8.4+) for readability. However, ?int remains perfectly valid and widely supported since 8.0.

When working with frameworks like Laravel, this feature aligns well with the broader shift toward strict typing found in the latest releases. If you are maintaining legacy code or writing packages that need to be compatible with older PHP versions, you must remember that this syntax is not available in PHP 7.x. Attempting to use ?int in a PHP 7 environment will result in a parse error because the parser does not recognize the question mark as part of the type declaration grammar at that version.

Ultimately, the question mark before a type is a concise contract between your function and its caller. It communicates that the absence of data (null) is a valid state within the scope of that parameter, preventing unnecessary runtime exceptions and clarifying intent to anyone reading the code.

Our network