Ffile2fix
Sign in Get started

How to fix "Too few arguments to function" in PHP

Fatal error: Uncaught ArgumentCountError: Too few arguments to function App\Mailer::send(), 1 passed in /var/www/html/app/Order.php on line 48 and exactly 2 expected

A function or method was called without all of its required parameters, so PHP threw an ArgumentCountError. Since PHP 7.1 this is a fatal error rather than a warning.

Also appears as: Uncaught ArgumentCountError: Too few arguments to function format_date(), 0 passed and at least 1 expected · Too few arguments to function App\Http\Controllers\PostController::show(), 1 passed and exactly 2 expected (Laravel) · ArgumentCountError: Too few arguments to function {closure}(), 1 passed · ArgumentCountError: str_replace() expects at least 3 arguments, 2 given

Common causes

  • A function signature gained a new required parameter but old call sites were not updated
  • WordPress add_action/add_filter registered with the default 1 accepted argument while the callback expects more
  • Laravel route parameters or dependency injection not matching the controller method
  • Callbacks passed to array_map, usort or event dispatchers with a different number of parameters
  • A plugin or library update changed a public method signature

How to fix it

  1. Compare the call to the definition. The message shows how many arguments were passed and how many are expected, plus the calling file and line. Open both.
  2. Pass the missing arguments. Update the call site to supply every required parameter in the right order, or use named arguments in PHP 8 for clarity.
  3. Add sensible defaults. If the new parameter is optional in practice, give it a default (string $cc = '') so existing calls keep working.
  4. Fix WordPress hook arg counts. Set the fourth argument of add_action/add_filter to the number your callback takes, e.g. add_action('save_post', 'my_fn', 10, 3).
  5. Match framework routing. In Laravel, make sure the route has a {placeholder} for every non-injected controller parameter, and that names match.
  6. Check for signature changes after updates. If the error appeared after updating a package, read its changelog and update your overrides or calls.

WordPress

// callback takes 3 args, so tell WordPress to pass 3
add_action('save_post', 'acme_on_save', 10, 3);

function acme_on_save(int $post_id, WP_Post $post, bool $update): void
{
    // ...
}

How to stop it happening again

  • Add new parameters with defaults to keep backward compatibility
  • Use an IDE or static analysis to flag mismatched calls
  • Pin and review major version updates of libraries you extend

Frequently asked questions

Why is it fatal now when older PHP only warned?

Since PHP 7.1, calling a user-defined function with too few arguments throws ArgumentCountError. PHP 8 also throws it for built-in functions.

What about passing too many arguments?

Extra arguments to user-defined functions are silently ignored. Built-in functions throw ArgumentCountError when given too many.

The error says {closure}. How do I find it?

Use the 'passed in' file and line or the stack trace to locate the anonymous function and the code that calls it, such as an array_map or event listener.