TypeError
PHP Programming Language
Severity: CriticalWhat Does This Error Mean?
A TypeError is thrown when a function receives an argument of the wrong data type, or when it returns a value of the wrong type. This became much more common in PHP 7 and 8 because modern PHP lets you declare exactly what types functions accept and return. For example, if a function expects an integer but you pass a string, PHP throws a TypeError.
Affected Models
- PHP 7.x
- PHP 8.x
Common Causes
- Passing a string to a function that declares an integer type hint: function add(int $a, int $b)
- Returning the wrong type from a function that has a return type declaration
- Passing null to a function parameter that is not declared as nullable
- Using strict_types=1 at the top of a file, which turns PHP's automatic type conversion off
- An array being passed where an object is expected, or vice versa
How to Fix It
-
Read the error message carefully. It tells you exactly which function was called, what type it expected, and what type it received.
Example: 'Argument 1 passed to multiply() must be of the type int, string given'. The fix is clear from the message.
-
Cast the value to the correct type before passing it: (int)$value, (float)$value, (string)$value.
Example: add((int)$userInput, 5); — this converts the user input to an integer before passing it.
-
If null is a valid value for the parameter, declare it nullable: function save(?string $name) — the ? makes it nullable.
Without the ?, passing null will throw a TypeError in PHP 8 strict mode.
-
If you are using declare(strict_types=1), PHP will not automatically convert types for you. You must pass exactly the right type.
Without strict_types, PHP converts '5' (string) to 5 (int) automatically. With it, the conversion does not happen and a TypeError is thrown.
-
If you are writing the function, consider using union types (PHP 8) to accept multiple types: function save(int|string $id).
Union types allow a parameter to accept more than one type, which is more flexible.
When to Call a Professional
TypeErrors are a normal part of modern PHP development. They are design-time errors that a PHP developer can fix by correcting the types being passed or making function signatures more flexible. If you encounter them in a third-party library, report them as bugs to that project.
Frequently Asked Questions
Did TypeErrors exist in PHP 5?
Not in the same way. PHP 5 had type hints for objects and arrays, but it did not throw TypeErrors for scalar types like int, string, and float. PHP 7 introduced scalar type hints and TypeErrors for them. PHP 8 made the type system even stricter.
What is strict_types in PHP?
Adding 'declare(strict_types=1);' at the very top of a PHP file turns off PHP's automatic type coercion. Without it, PHP converts '5' to 5 automatically. With it, passing the wrong type always throws a TypeError. It makes your code more predictable and easier to debug.
Is a TypeError catchable?
Yes. You can wrap the call in a try/catch block and catch TypeError. try { result = divide($a, $b); } catch (TypeError $e) { echo 'Invalid input type'; } This lets you handle type problems gracefully without crashing the whole script.