TypeError: slugify(): Argument #1 ($title) must be of type string, null given
PHP · updated 2026-11-13
Thrown when you pass null to a user-defined function whose parameter is typed string (or int, array...) without a ?. It happens in every PHP 8 mode, strict_types or not. PHP's own functions behave differently: trim(null) only raises the deprecation "Passing null to parameter #1 ($string) of type string is deprecated" (8.1 and later).
PHP Fatal error: Uncaught TypeError: slugify(): Argument #1 ($title) must be of type string, null given, called in /app/src/Post.php on line 5 and defined in /app/src/Post.php:3
Stack trace:
#0 /app/src/Post.php(5): slugify(NULL)
#1 {main}
thrown in /app/src/Post.php on line 3Quick fix
The message names the function and the argument, and "called in ... on line N" is where the null came from. Look at that call: usually an array key that is missing or null (a database column, a request field). Decide whether null is valid: if it is, type the parameter ?string and handle it; if not, check the value where it is read and fail with a clear message, or give it a default with ??.
- Read "called in <file> on line N": that is the call site that passed null.
- Find the value: an array key, a nullable database column, a missing request field, a function that returns null on failure.
- If null is a valid value, change the parameter to ?string (or give it a default) and handle the null inside.
- If it is not, validate at the boundary (request validation, a NOT NULL column) and pass a real string, or use $value ?? '' where an empty string is right.
<?php
// null is allowed: say so in the type and handle it
function slugify(?string $title): string
{
return strtolower(trim($title ?? ''));
}
// null is a bug: stop it where the value is read
$title = $post['title'] ?? throw new InvalidArgumentException("post {$post['id']} has no title");
echo slugify($title);Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.
Read https://runheed.com/errors/php-must-be-of-type-string-null-given.md and fix this error in this project.Why it happens
An array key is missing or null
$row['title'] is null for a nullable database column or a record created before the column was filled; a missing key gives null plus an "Undefined array key" warning.
Request input that was not sent
$request->input('title') and $_POST['title'] ?? null return null when the field is absent, and the value flows into a typed function.
A function returns null on failure
json_decode on bad JSON, a repository find() for a missing record, or preg_replace on error return null, and the next typed call throws.
Upgrading to PHP 8 or adding types
Code that passed null into untyped parameters for years starts throwing once the parameter gets a string type.
Other fixes
- Run PHPStan or Psalm at a level that reports possibly-null arguments before deploy.
- Fix the "Passing null to parameter" deprecations for built-in functions too: they become errors in a future PHP version.
- In Laravel, validate the request (required|string) so a missing field is a 422 instead of a TypeError.
Same error, other wording
- TypeError: App\Support\Str::slug(): Argument #1 ($title) must be of type string, null given
- TypeError: Argument #1 ($value) must be of type array, null given
- Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated
Let your coding agent catch the next one
Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:
> Find the "must be of type string, null given" TypeErrors in production, open the latest one, follow "called in" to where the null came from, and either allow null in the type or stop it where the value is read.
Setup for PHP, then have your agent fix and resolve it.
Related errors
- AttributeError: 'NoneType' object has no attribute 'x'
- TypeError: Cannot read properties of undefined (reading 'x')
- java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because the return...
More in Undefined, None and type errors
- panic: assignment to entry in nil mapGo
- System.InvalidOperationException: Sequence contains no elements.NET
- panic: runtime error: invalid memory address or nil pointer dereferenceGo
- NoMethodError: undefined method 'email' for nilRuby
- System.NullReferenceException: Object reference not set to an instance of an object..NET
- TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.Node.js
- TypeError: Cannot convert undefined or null to objectNode.js
- TypeError: items is not iterableNode.js
- TypeError: x is not a functionNode.js
- TypeError: Client is not a constructorNode.js
- ReferenceError: x is not definedNode.js
- ERR_INVALID_ARG_TYPE: The "path" argument must be of type string. Received undefinedNode.js
- TypeError: 'NoneType' object is not subscriptablePython
- TypeError: 'NoneType' object is not iterablePython
- KeyError: 'email'Python
- TypeError: unhashable type: 'list'Python
- AttributeError: 'dict' object has no attribute 'name'Python
- IndexError: list index out of rangePython
- TypeError: unsupported operand type(s) for +: 'int' and 'str'Python
- ValueError: invalid literal for int() with base 10: ''Python
- TypeError: __init__() got an unexpected keyword argument 'proxies'Python
- UnboundLocalError: cannot access local variable 'total' where it is not associated with a...Python
- RuntimeError: dictionary changed size during iterationPython
- ValueError: too many values to unpack (expected 2)Python
- TypeError: 'str' object is not callablePython
References: PHP manual: Type declarations (nullable types), PHP RFC: Deprecate passing null to non-nullable arguments of internal functions
Spotted a mistake? Email hello@greatwork.company. About Runheed.