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 3

Quick 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 ??.

  1. Read "called in <file> on line N": that is the call site that passed null.
  2. Find the value: an array key, a nullable database column, a missing request field, a function that returns null on failure.
  3. If null is a valid value, change the parameter to ?string (or give it a default) and handle the null inside.
  4. 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
<?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);
Fix it with your coding agent

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.

prompttext
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

More in Undefined, None and type errors

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.