TypeError: Wrong Argument Type

Fatal error: Uncaught TypeError: Argument #1 ($name) must be of type string, null given

Quick Answer

You are passing a value of the wrong type to a function with type declarations. Ensure the argument matches the expected type or make the parameter nullable.

Why This Happens

PHP 7+ enforces type declarations in function signatures when strict_types is enabled or for built-in type coercion failures. When you pass null to a string parameter or an array to an integer parameter, PHP throws a TypeError. This is especially common when database queries return null for missing fields.

The Problem

function createUser(string $name, string $email): void {
    // ...
}
$name = $_POST['name'] ?? null;
createUser($name, 'test@example.com');

The Fix

function createUser(string $name, string $email): void {
    // ...
}
$name = $_POST['name'] ?? '';
if ($name === '') {
    throw new InvalidArgumentException('Name is required');
}
createUser($name, 'test@example.com');

Step-by-Step Fix

  1. 1

    Read the type mismatch

    The error message tells you which argument, what type was expected, and what type was actually given.

  2. 2

    Trace the argument value

    Find where the variable gets its value and determine why it has the wrong type. Check for null returns, missing input, or incorrect casts.

  3. 3

    Fix the type or the declaration

    Either validate and convert the value before passing it, or change the parameter to a nullable type (?string) if null is a valid input.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free