Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Exceptions and Errors

The PHP ArgumentCountError class

Reference for PHP ArgumentCountError: thrown when a function gets too few arguments (or the wrong count for a built-in), with causes, examples and fixes.

ArgumentCountError is thrown when a function is called with fewer arguments than it requires. It extends TypeError, so a catch (TypeError $e) block also catches it. Before PHP 7.1 this situation was only a warning and the function ran with missing values.

There is an asymmetry worth remembering. For functions you write, passing too many arguments is allowed and the extras are ignored (you can read them with func_get_args()), while too few throws. Built-in functions are stricter: both too few and too many throw.

PHP
function greet(string $name, string $greeting) { /* ... */ }
greet("Ada"); // ArgumentCountError

Try it

PHP Too few and too many arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Named arguments, callbacks and the class hierarchy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
class ArgumentCountError extends TypeError {}

It adds no methods or properties; the message tells you which function, how many arguments it expected and how many it got.

Message pattern When
Too few arguments to function greet(), 1 passed in ... and at least 2 expected A user function got fewer than its required count.
strlen() expects exactly 1 argument, 0 given A built-in got the wrong number of arguments.

Return value: not applicable; it is an exception class.

Usage notes

  • A bug, not an input problem. It is raised by the call site, not by the data. Fix the call, give the parameter a default value, or make the parameter variadic.
  • Defaults and nullable types are the usual cure: function f(string $a, ?string $b = null). Note that ?string $b without a default is still required.
  • Callbacks are a frequent source: array_map() passes one element per array, array_filter() passes the value (and optionally the key), usort() passes two items. A callback that requires more parameters than the caller supplies throws this error.
  • Extra arguments to user functions are silently ignored, which can hide typos; static analysis tools and declare(strict_types=1) help catch them, and func_num_args() reveals what was passed.
  • Built-in functions with optional parameters accept the documented range only; a count outside it throws.
  • Class constructors count too: new Point(1) for a constructor needing two values throws.
  • Catch rarely. A catch for TypeError will also catch this subclass, which may hide a calling mistake; handle it only in code that executes untrusted callbacks.