Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated

24.9k Views Asked by At

We are using the following functions for an Invoice Module:

    /* Functions: Before */
    function before ($here, $inthat){
        return substr($inthat, 0, strpos($inthat, $here));
    }
    /* Functions: After */
    function after ($here, $inthat){
        if (!is_bool(strpos($inthat, $here)))
        return substr($inthat, strpos($inthat,$here)+strlen($here));
    }   
    /* Functions: Between */
    function between ($here, $that, $inthat){
        return before ($that, after($here, $inthat));
    }

This code is showing following Error Message in PHP 8.1:

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated

Please guide us as to what to do in this regard.

Thanks,

Lakshmanan

1

There are 1 best solutions below

2
celextel On

Error Messages are shown for the following code:

return substr($inthat, 0, strpos($inthat, $here));

We are getting the following Error Messages:

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /home/XXX/functions_general.php on line 10
Deprecated: substr(): Passing null to parameter #1 ($string) of type string is deprecated in /home/XXX/functions_general.php on line 10

We post the following Comments [showing only short example] in the Online Order [osCommerce earlier and Zen Cart now]:

Invoice No. TI-220013 | Invoice Date: 12-Oct-2022 |

We capture these values through the following code:

if ($invoice_details != null) {
    $invoice_number = between('Invoice No. ', '|', $invoice_details);
    $invoice_number = trim($invoice_number);
    $invoiced_date = between('Invoice Date: ', '|', $invoice_details);
    $invoiced_date = trim($invoiced_date);
}

All the values are shown correctly in spite of the Error Messages.

We have hidden Error Messages by changing the above code as follows:

return @substr($inthat, 0, strpos($inthat, $here));

Any other suggestion is welcome.

Thanks,

Lakshmanan