ArgumentCountError: a function was called with too few arguments
Fatal error: Uncaught ArgumentCountError: Too few arguments to functionShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The message says 1 passed and exactly 2 expected, and the call site has one value.The call simply leaves out a required argumentFix - 2
PHP 8 also prints a deprecation notice for the signature before the Error.A required parameter was placed after an optional oneFix - 3
The signature is function foo(?string $bar) and the call is foo().A nullable type does not make the argument optionalFix
- 1
What does the error look like?
Fatal error: Uncaught ArgumentCountError: Too few arguments to functionToo 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.phpConfirmed by php.net
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.php2Fix
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
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/ | head2Fix
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); // ok3Verify
Call the function with no arguments.
php -r 'require "foo.php"; foo(); echo "ok";'Confirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix