search
php

Fix: Deprecated: Required parameter follows optional parameter error PHP

Quick fix for 'Deprecated: Required parameter follows optional parameter' error in PHP. Learn how to properly order function parameters.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 2 min read
PHP Functions Parameters Error Fix Deprecation

The ‘Deprecated: Required parameter follows optional parameter’ error occurs in PHP 8.0+ when a required parameter is placed after an optional parameter in function declarations.


How the Error Happens

❌ Error Scenario:

<?php
// ❌ This causes the deprecation error
function createUser($name, $email = 'default@example.com', $age) {
    // PHP 8.0+: Deprecated: Required parameter $age follows optional parameter $email
}
?>

✅ Quick Fix - Order Function Parameters Properly

Solution 1: Reorder Parameters

<?php
// ✅ Move required parameters before optional ones
function createUser($name, $age, $email = 'default@example.com') {
    return "Name: $name, Age: $age, Email: $email";
}

// ✅ Usage
echo createUser('John', 25); // Name: John, Age: 25, Email: default@example.com
echo createUser('Jane', 30, 'jane@example.com'); // Name: Jane, Age: 30, Email: jane@example.com
?>

Solution 2: Make All Parameters Optional

<?php
// ✅ Provide defaults for all parameters
function createUser($name = '', $email = 'default@example.com', $age = 0) {
    return "Name: $name, Age: $age, Email: $email";
}

// ✅ Usage
echo createUser('John', 'john@example.com', 25);
?>

Solution 3: Use Null Coalescing Operator

<?php
// ✅ Use null coalescing for required parameters
function createUser($name, $email = null, $age = null) {
    $email = $email ?? 'default@example.com';
    $age = $age ?? 0;
    
    return "Name: $name, Age: $age, Email: $email";
}

// ✅ Usage
echo createUser('John', null, 25);
?>

Solution 4: Use Named Arguments (PHP 8.0+)

<?php
// ✅ Use named arguments to bypass parameter order
function createUser($name, $email = 'default@example.com', $age = 0, $role = 'user') {
    return "Name: $name, Age: $age, Email: $email, Role: $role";
}

// ✅ Usage with named arguments
echo createUser(
    name: 'John',
    age: 25,
    role: 'admin'
);
?>
Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

php

Fix: Class not found in Laravel error PHP fix

Quick fix for 'Class not found' error in Laravel. Learn how to resolve autoloading and namespace issues in Laravel applications.

January 8, 2026
php

Fix: Composer command not found PHP error in terminal - Quick Solution

Quick fix for 'Composer command not found' PHP error in terminal. Learn how to properly install and configure Composer on Windows, Mac, and Linux.

January 8, 2026
php

Fix: file_get_contents(): failed to open stream PHP error

Quick fix for 'file_get_contents(): failed to open stream' error in PHP. Learn how to properly handle file operations and remote requests.

January 8, 2026