php 8.1 - explode(): Passing null to parameter #2 ($string) of type string is deprecated

php 8.1 - explode(): Passing null to parameter #2 ($string) of type string is deprecated

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

php 8.1 - explode(): Passing null to parameter #2 ($string) of type string is deprecated

PHP 8.1 introduced a wave of deprecation notices that caught many developers off guard, particularly those upgrading from version 8.0 or earlier. One of the most persistent errors you will encounter is: explode(): Passing null to parameter #2 ($string) of type string is deprecated. This specific warning often appears when your code attempts to split a string that contains no value at all.

Consider a common scenario where you are processing an API response or a database field. You might write code like this:

// explode uids into an array
$comp_uids = explode(',', $result['comp_uids']);

At first glance, this logic seems sound. If $result['comp_uids'] is empty (an empty string ""), explode should simply return an array containing one element: that empty string. However, if the key 'comp_uids' does not exist in the $result array, or if the data source returns null, the variable holds a null value. Passing null to explode was previously allowed because it implicitly treated the null as an empty string. PHP 8.1 explicitly forbids this implicit conversion for specific functions, signaling that your code is relying on behavior that might break unexpectedly or silently mask data integrity issues.

The reason behind this deprecation goes deeper than just stricter typing. Implicit type juggling—where null becomes "" without you asking—has historically been a source of security vulnerabilities and logic bugs. By forcing developers to handle null explicitly, PHP encourages more predictable behavior. If your code crashes or behaves differently based on whether a field is missing versus being empty, that is usually a sign that the data structure itself is inconsistent, not a minor edge case to be ignored with an implicit cast.

To resolve this deprecation notice, you must stop relying on the function's ability to coerce null into a string. The most robust solution involves validating the input before passing it to explode. You can check if the variable is null or empty using a conditional check or the null coalescing operator.

Here is a corrected approach:

// Safely handle null or missing data
$rawValue = $result['comp_uids'] ?? '';

if (empty($rawValue)) {
    // Treat as no IDs present, or initialize with an empty array depending on logic needs
    $comp_uids = []; 
} else {
    $comp_uids = explode(',', $rawValue);
}

Alternatively, using the null coalescing operator directly within a conditional ensures that you never pass null to explode:

$comp_uids = explode(',', $result['comp_uids'] ?? '');

While this second snippet looks cleaner, it still results in an empty array if the input is truly missing. You must decide what semantic meaning "missing data" holds for your application. If a missing comp_uids field means "no companies selected," returning an empty array is correct. If it means "error," you should throw an exception before calling explode.

This same pattern applies to other deprecated functions in PHP 8.1, such as strlen(), stripos(), and strpos(). These functions now require a strictly typed string parameter. If you encounter the error strlen(): Passing null to parameter #1 ($string) of type string is deprecated, apply the same logic: ensure the variable passed is never null before the function call.

In larger applications, especially those built on frameworks like Laravel, this often points to a need to update your validation rules or default value configurations. If you are working with API responses where fields might be omitted entirely rather than set to null, using the null coalescing operator (??) is essential to normalize the data before processing. This ensures that your string manipulation functions receive consistent input types, preventing runtime deprecation warnings and future compatibility issues.

Our network