Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated
Starting with PHP 8.1, the behavior of strpos() has changed significantly regarding how it handles the $haystack parameter. Previously, passing a null value would return -1 without raising an error or warning. Now, PHP enforces strict typing and issues a deprecation notice: "Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated." If you ignore this now, it will become a full-fledged Error in PHP 8.2.
This specific error often surfaces in legacy codebases that rely on implicit truthiness checks or assume strings are always present. The core issue lies in how your helper functions handle cases where the search term is found at the very beginning of the string or not found at all, combined with how you are constructing the input for subsequent calls.
Analyzing the Logic Flow
Let's look closely at the three functions provided in your Invoice Module scenario:
before($here, $inthat): This attempts to find$hereinside$inthatand returns everything preceding it.after($here, $inthat): This checks ifstrposreturns a boolean (which implies-1in older PHP versions), and if not, it slices the string after the match.between($here, $that, $inthat): This composes the first two functions to extract text between$hereand$that.
The error message indicates that strpos() is receiving a null value for its first argument in at least one execution path. In PHP 8.1+, passing null here triggers the deprecation warning.
The Root Cause in after()
The primary culprit in your snippet is the after() function:
function after ($here, $inthat){
if (!is_bool(strpos($inthat, $here)))
return substr($inthat, strpos($inthat,$here)+strlen($here));
}
In PHP versions prior to 8.1, strpos() returned -1 (integer) when the needle was not found, and false (boolean) on other failures. In PHP 8+, strpos() returns false only if the needle is not found, but it strictly expects a string for the haystack.
However, the logic inside after() relies on checking !is_bool(strpos($inthat, $here)). This check was likely written assuming that strpos() could return -1 (integer) or false (boolean). While this specific conditional logic might not be the direct cause of passing null into strpos, it suggests a pattern of relying on integer returns from strpos().
But where does the null come from? It usually happens when the $inthat variable itself is empty or missing in certain execution paths, or if $here becomes null. If $inthat is an empty string, strpos works fine. If $inthat is null, that is where you hit the wall.
Fixing the after() Function
To resolve this immediately and future-proof your code for PHP 8.2+, you must ensure that $inthat is never null before passing it to strpos(). Additionally, you should stop relying on is_bool() to detect a "not found" state if you intend to use the integer return value later, although modern PHP allows checking against false directly.
Here is a robust refactor of the after() function:
function after ($here, $inthat) {
// Ensure $inthat is treated as a string to prevent null warnings in PHP 8.1+
if (!is_string($inthat)) {
return '';
}
// strpos returns false if not found, or an integer offset >= 0
$pos = strpos($inthat, $here);
// If not found (false), return empty string to avoid invalid substr calls
if ($pos === false) {
return '';
}
return substr($inthat, $pos + strlen($here));
}
Addressing the before() Function
The before() function also needs a safety check. If $inthat is null, or if the split logic fails unexpectedly, you could face similar issues.
function before ($here, $inthat) {
// Cast to string to handle potential null inputs gracefully
$haystack = (string)$inthat;
// Check if needle exists in haystack
if ($haystack === '' || strpos($haystack, $here) === false) {
return '';
}
$pos = strpos($haystack, $here);
return substr($haystack, 0, $pos);
}
The between() Composition
Once before and after are hardened to reject or convert null inputs safely, the between function will naturally work. However, there is a logical edge case: what if $here appears after $that? Or what if they are adjacent? The current logic passes the result of after() into before().
If after() returns an empty string (because the needle wasn't found), passing that to before() as the second argument ($inthat) should be safe now, provided we cast it to a string.
function between ($here, $that, $inthat) {
// Refactor using the robust versions of before and after
return before($that, after($here, (string)$inthat));
}
Why This Matters for Your Invoice Module
In an invoice context, string manipulation is critical. You are likely parsing delimited fields or extracting specific line items. If your logic assumes a certain structure exists in every row of your database, strpos returning -1 vs false can break downstream calculations if not handled strictly.
By explicitly casting $inthat to (string) and checking for false return values from strpos, you align your code with the strict typing requirements introduced in PHP 8.1. This prevents unexpected deprecation warnings that could clutter your error logs and ensures compatibility as you upgrade to PHP 8.2.
For more advanced string manipulation needs in Laravel applications, consider using Laravel's built-in collection helpers or dedicated parsing libraries which handle edge cases like missing delimiters internally. While custom functions offer flexibility, they require rigorous null safety checks exactly as demonstrated above.