"Uncaught TypeError: Argument #1 ($x) must be of type X" in PHP — What It Means and How to Fix It

What the PHP error "Uncaught TypeError: Argument #1 ($x) must be of type X" means and how to fix it.

Published November 1, 2026

A function with typed parameters received a value of the wrong type. With strict_types=1 PHP refuses to coerce between types.

What it means and why it happens

Typical causes: form input arrives as strings, null passed to a non-nullable parameter, or arrays where an object is expected.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
Fatal error: Uncaught TypeError: foo(): Argument #1 ($x) must be of type int, string given

Practical examples

Code that triggers it

<?php
declare(strict_types=1);
function add(int $a, int $b) { return $a + $b; }
add('5', 3);   // string given

The fix

<?php
declare(strict_types=1);
function add(int $a, int $b): int { return $a + $b; }
add((int) '5', 3);

Nullable and union types

function find(?int $id, int|string $key) { /* ... */ }

Common mistakes

  • Passing raw $_GET/$_POST strings to typed params.
  • Passing null without ?type.
  • Disabling types instead of fixing callers.

Key takeaways

  • TypeError: Argument must be of type: A function with typed parameters received a value of the wrong type.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

Does PHP convert types automatically?

Only in non-strict mode and only for scalars.

How do I find which line caused TypeError: Argument must be of type?

The error message and stack trace show the file and line; start there and work backwards.

More PHP articles