> ## 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 Typed Parameter

PHP typed parameters (formally known as type declarations) are explicit constraints applied to function or method signatures that dictate the expected data type of incoming arguments. When a parameter is typed, the PHP engine enforces type checking at runtime, throwing a `TypeError` exception if the provided argument does not match the declared type or cannot be safely coerced into it.

```php theme={"dark"}
function functionName(Type $parameterName) {
    // ...
}
```

## Type Resolution and Coercion

PHP handles typed parameters in two distinct modes:

1. **Coercive Mode (Default):** PHP utilizes weak typing. If an argument's type does not match the declaration, PHP will attempt implicit type coercion to cast the value to the expected type without data loss (e.g., casting the string `"123"` to an `int`).
2. **Strict Mode:** Enabled by declaring `declare(strict_types=1);` as the very first statement in a file. In strict mode, PHP disables type coercion. The engine requires an exact type match for all function calls made within that file, with the single exception that an `int` can be passed to a `float` parameter.

## Supported Type Declarations

PHP supports a comprehensive type system for parameters, categorized as follows:

* **Scalar Types:** `int`, `float`, `string`, `bool`.
* **Compound Types:** `array`, `iterable`, `object`.
* **Class/Interface Types:** Any valid class or interface name (e.g., `DateTime`, `MyCustomInterface`).
* **Special Types:**
  * `callable`: Accepts any valid PHP callable (functions, object methods, static class methods, closures).
  * `mixed` *(PHP 8.0+)*: Represents the union of `object|resource|array|string|float|int|bool|null`.

## Advanced Type Constructs

Modern PHP introduces complex type definitions for parameters:

**Nullable Types**
Prefixing a type declaration with a question mark (`?`) allows the parameter to accept either the specified type or `null`.

```php theme={"dark"}
function setIdentifier(?string $id) {}
```

**Union Types (PHP 8.0+)**
Allows a parameter to accept one of multiple specified types, separated by a pipe (`|`).

```php theme={"dark"}
function processInput(int|float|string $input) {}
```

**Intersection Types (PHP 8.1+)**
Requires an object argument to satisfy multiple class or interface constraints simultaneously, separated by an ampersand (`&`). This is exclusively for class/interface types.

```php theme={"dark"}
function iterateCollection(Traversable&Countable $collection) {}
```

**Disjunctive Normal Form (DNF) Types (PHP 8.2+)**
Allows the combination of Union and Intersection types within a single parameter declaration. Intersection types must be grouped with parentheses.

```php theme={"dark"}
function handleData((Traversable&Countable)|array $data) {}
```

## Interaction with Default Values

When a typed parameter is assigned a default value, the default value must strictly satisfy the declared type. Historically, assigning a default value of `null` implicitly made the parameter nullable. However, as of PHP 8.4, this implicit nullability is deprecated; developers must explicitly use the `?` prefix or include `null` in a union type.

```php theme={"dark"}
// Valid
function configure(bool $isVerbose = false) {}

// Valid (Explicit nullability)
function setLimit(?int $limit = null) {}

// Deprecated in PHP 8.4+ (Implicit nullability)
function setOffset(int $offset = null) {}
```

<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>
