Required parameter $xxx follows optional parameter $yyy

Required parameter $xxx follows optional parameter $yyy

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

Stefan Bogdanescu

Founder & Senior Architect

Required parameter $xxx follows optional parameter $yyy

Starting with PHP 8.0, the runtime behavior for function signatures changed regarding how default values are interpreted. If you encounter a Deprecated: Required parameter $xxx follows optional parameter $yyy warning during execution, it is because your code violates a stricter rule introduced in this version. This specific error triggers when a required parameter appears after an optional one in a single function definition.

Consider the following example that fails under PHP 8.0:

function test_function(int $var1 = 2, int $var2) {
    return $var1 / $var2;
}

In versions prior to 8.0, this syntax was accepted without issue. The interpreter would simply use the default value of 2 for $var1 if not provided, and require a value for $var2. However, PHP 8.0 enforces a strict ordering: parameters with default values must always precede parameters without them.

The logic behind this restriction is rooted in how function calls are resolved. When you invoke a function, the arguments map to parameters sequentially from left to right. If you omit the first argument, the parser assumes it should use the default value for that parameter. If the next parameter is required, the parser has no way to distinguish between an omitted optional argument and a missing required one without strict ordering guarantees.

By enforcing that all optional parameters come before all required ones, PHP 8.0 eliminates this ambiguity. This change aligns with best practices found in many statically typed languages and simplifies the mental model for developers reading function signatures. It ensures that if you decide to omit arguments when calling a function, every omitted argument corresponds to a parameter that explicitly accepts a default.

To resolve this deprecation warning, you must reorder your parameters so that required ones come first, or assign defaults to all parameters in the function signature. Here are two valid approaches:

Approach 1: Reordering Parameters
If the logic allows it, move the required parameter before the optional one.

function test_function(int $var2, int $var1 = 2) {
    return $var1 / $var2;
}

In this version, $var2 is strictly required. You must provide a value for it when calling the function. If you omit $var1, it defaults to 2. This satisfies the new rule because the optional parameter follows only after all required parameters have been defined.

Approach 2: Providing Defaults for All Parameters
Alternatively, you can assign a default value to every parameter. This makes the entire function call optional in terms of argument passing, which is often safer for API design.

function test_function(int $var1 = 2, int $var2 = 0) {
    return $var1 / $var2;
}

Note that while this fixes the syntax error, it introduces a new runtime risk: division by zero if $var2 is never provided. When refactoring legacy code, always verify that adding defaults does not introduce subtle bugs related to unexpected null or zero values.

If you are maintaining a large codebase, you may face hundreds of these warnings immediately upon upgrading. A practical strategy is to suppress the specific deprecation error temporarily while you refactor, then systematically fix the signatures. You can use #[\ReturnTypeWillChange] for traits or specific error suppression tactics if necessary, though cleaning up the signatures is the only permanent solution.

For projects built on Laravel, this change often surfaces when upgrading from PHP 7.4 to 8.0 or later. The framework itself adheres to these new standards, so any custom service classes or legacy controllers you maintain will need adjustment. If you rely on third-party packages that were optimized for older PHP versions, check their documentation for updated signatures.

While this break in compatibility might seem inconvenient, the strictness of PHP 8.0 significantly reduces runtime errors caused by argument confusion. By adhering to the rule that optional parameters must precede required ones, your code becomes more predictable and easier to debug. This shift reflects a broader trend in modern software engineering: explicit over implicit, where the language structure forces clarity in how data flows into functions.

Our network