Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ArgumentCountError: a function was called with too few arguments

ErrorFatal error: Uncaught ArgumentCountError: Too few arguments to function

Short answer

PHP throws ArgumentCountError when a function or method is called without a value for a required parameter. The message gives how many were passed and how many are expected. Either pass the missing argument, give the parameter a default value, or fix the parameter order so required ones come first.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the function named in the message, open its definition and count the parameters that have no default, then look at the call site in the stack trace.

    grep -rn "function makeyogurt" src/
    grep -rn "makeyogurt(" src/
  2. 2Match what you see, then jump to its fix

    1. 1The message says 1 passed and exactly 2 expected, and the call site has one value.The call simply leaves out a required argumentFix
    2. 2PHP 8 also prints a deprecation notice for the signature before the Error.A required parameter was placed after an optional oneFix
    3. 3The signature is function foo(?string $bar) and the call is foo().A nullable type does not make the argument optionalFix

What does the error look like?

  • Fatal error: Uncaught ArgumentCountError: Too few arguments to function
  • Too few arguments to function makeyogurt(), 1 passed

Cause 1 of 3

The call simply leaves out a required argument

1Check

Since PHP 7.1 a missing parameter without a default throws; older versions only warned. Compare the call with the signature.

grep -rn "makeyogurt(" src/

2Fix

Pass every required argument, or give the parameter a default if it really is optional.

function makeyogurt($flavour, $container = "bowl") {
    return "Making a $container of $flavour yogurt.\n";
}
echo makeyogurt("raspberry");

3Verify

Run the call again; no Error is thrown.

php yogurt.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A required parameter was placed after an optional one

1Check

In function f($a = 1, $b), $a cannot be skipped by position, so f(5) has one value for two parameters. PHP 8.0 deprecates this order.

php -l src/Yogurt.php

2Fix

Put the required parameters first, as the manual's own example does, or drop the useless default.

// before
function makeyogurt($container = "bowl", $flavour) {}
// after
function makeyogurt($flavour, $container = "bowl") {}

3Verify

Call it with one argument.

php -r 'require "yogurt.php"; echo makeyogurt("raspberry");'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A nullable type does not make the argument optional

1Check

The ? only allows null as a value. The argument must still be passed. Only a default value makes it optional.

grep -rn "?string\|?int\|= null" src/ | head

2Fix

Pass null explicitly, or add = null as the default (write the type as ?string $bar = null).

function foo(?string $bar = null) { /* ... */ }
foo();        // ok
foo(null);    // ok

3Verify

Call the function with no arguments.

php -r 'require "foo.php"; foo(); echo "ok";'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Give optional parameters a default and keep them last.
  • Run a static analyser (PHPStan or Psalm) in CI; it reports wrong argument counts before runtime.
  • Use named arguments for functions with many optional values.
  • Run php -l and your tests after changing any function signature.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.