Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

ArgumentCountError

ArgumentCountError extends TypeError.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

try { strlen("a", "b"); } catch (ArgumentCountError $e) { /* ... */ }

ArgumentCountError extends TypeError. In PHP 8.4 it is thrown for too few required arguments to a user function or excessive positional arguments to a non-variadic built-in. Extra positional arguments to a user-defined function can still be read with func_get_args. A zero-argument call in the example fails before the function body runs.

function first($value) { return count(func_get_args()); }
foreach ([fn() => first(), fn() => strlen("x", "y")] as $call) {
    try { $call(); } catch (ArgumentCountError $e) {
        echo get_class($e), " ", ($e instanceof TypeError ? "TypeError" : "other"), PHP_EOL;
    }
}
echo first("A", "B");
ArgumentCountError TypeError
ArgumentCountError TypeError
2

Examples

<?php
function first($value) { return count(func_get_args()); }
foreach ([fn() => first(), fn() => strlen("x", "y")] as $call) {
    try { $call(); } catch (ArgumentCountError $e) {
        echo get_class($e), " ", ($e instanceof TypeError ? "TypeError" : "other"), PHP_EOL;
    }
}
echo first("A", "B");

Result

ArgumentCountError TypeError
ArgumentCountError TypeError
2

Sources