> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP Nullable Parameter

A nullable parameter in PHP is a type declaration that explicitly permits a function or method argument to accept either a specified data type or the `null` value. It modifies the type signature to bypass strict type enforcement exclusively for `null`, preventing a `TypeError` exception when an absence of value is passed to the routine.

## Syntax and Implementation

PHP provides two primary syntactic constructs for defining nullable parameters, depending on the PHP version.

**1. The Nullable Prefix Operator (`?`)**
Introduced in PHP 7.1, prefixing a type declaration with a question mark explicitly marks it as nullable.

```php theme={"dark"}
function processString(?string $input): void {
    var_dump($input);
}

processString("Data"); // string(4) "Data"
processString(null);   // NULL
```

**2. Union Types (`|null`)**
Introduced in PHP 8.0, the union type syntax allows `null` to be appended to one or more types. At the engine level, `?Type` is internally resolved as `Type|null`.

```php theme={"dark"}
function processInteger(int|null $input): void {
    var_dump($input);
}

// Combined with multiple types
function processMixed(string|int|null $input): void {
    var_dump($input);
}
```

## Explicit vs. Implicit Nullability

Historically, PHP allowed **implicit nullability** by assigning `null` as the default value to a strictly typed parameter.

```php theme={"dark"}
// Implicit nullability (Deprecated as of PHP 8.4)
function legacyProcess(string $input = null): void {}
```

Modern PHP standards require **explicit nullability**. If a parameter has a default value of `null`, the type declaration itself must explicitly include the nullable operator or union type.

```php theme={"dark"}
// Explicit nullability with a default value (Standard)
function modernProcess(?string $input = null): void {}
function modernUnionProcess(string|null $input = null): void {}
```

## Interaction with Strict Typing

When `declare(strict_types=1);` is active, nullable parameters enforce exact type matching. The PHP engine will not coerce falsy values (such as `false`, `0`, or `""`) into `null`. The argument passed must strictly evaluate to the declared type or exactly `null`.

```php theme={"dark"}
declare(strict_types=1);

function evaluateState(?bool $state): void {}

evaluateState(true);  // Valid
evaluateState(null);  // Valid
evaluateState(0);     // Fatal error: Uncaught TypeError
```

## Variadic Nullable Parameters

When applying nullability to variadic parameters (`...`), the nullability applies to the individual elements within the unpacked array, not the array itself.

```php theme={"dark"}
function processVariadic(?string ...$inputs): void {
    // $inputs is evaluated as array<int, string|null>
}

processVariadic("A", null, "B"); // Valid
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
