2

Can anyone tell me how to resolve the below error. "Deprecated Functionality: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated"

I am using magento 2.4.4 with php 8.1

Check full error message here https://prnt.sc/tT33R-c4w3SK.

Below is the line causing error.

fromDate = date("Y-m-d",strtotime(str_replace("/", "-".$hseHelper->getNewYearBallFromDate())));
$toDate  = date("Y-m-d",strtotime(str_replace("/", "-", $hseHelper->getNewYearBallToDate())));
2
  • Please share you full error message here or screenshot of it.
    – Msquare
    Nov 18, 2022 at 9:11
  • please check my edited post Nov 18, 2022 at 9:53

3 Answers 3

3

That error indicates somewhere in your code passing null to the third parameter when calling a PHP function that is deprecated in PHP 8.1.

Assume you have below code:

return sprintf(
    $path,
    str_replace('methods_', '', $method)
);

The type of the third parameter should be changed to string if it is null. So the fixed code looks like the below:

return sprintf(
    $path,
    str_replace('methods_', '', $method ?? '')
);

The solution for your code: Change app/code/Velanapps/Hse/view/frontend/templates/new_year_ball.phtml on line 39 to the following

$fromDate = date("Y-m-d",strtotime(str_replace("/", "-", $hseHelper->getNewYearBallFromDate() ?? '')));
$toDate  = date("Y-m-d",strtotime(str_replace("/", "-", $hseHelper->getNewYearBallToDate() ?? '')));
8
  • please check my edited post Nov 18, 2022 at 9:53
  • It's a typos when you add your code into your question, I've check and updated my answer, please give it a try. The error will gone
    – Tu Van
    Nov 18, 2022 at 10:08
  • Can you please help me with this too magento.stackexchange.com/questions/361394/… Nov 23, 2022 at 5:35
  • Which Magento version (specific version, eg 2.4.3-p3) you are using for that project?
    – Tu Van
    Nov 23, 2022 at 5:48
  • its 2.4.4....... Nov 23, 2022 at 5:58
0

When you get this type of error then you should use this

If string requires

 $var ?? ''

If array requires

 $var ?? []

If number require

 $var ?? <addDefaultNumber>

If a Number type require and you have added a string type then (22 and '22' are different)

intval($var)

str_replace(): Argument #3 ($subject) must be of type array|string, float given

$float = 0.123;
$string = sprintf("%.2f", $float);
1
  • please check my edited post Nov 18, 2022 at 9:53
0

OLD

fromDate = date("Y-m-d",strtotime(str_replace("/", "-".$hseHelper->getNewYearBallFromDate())));
$toDate  = date("Y-m-d",strtotime(str_replace("/", "-", $hseHelper->getNewYearBallToDate())));

NEW

fromDate = date("Y-m-d",strtotime(str_replace("/", "-".$hseHelper->getNewYearBallFromDate() ?? '')));
$toDate  = date("Y-m-d",strtotime(str_replace("/", "-", $hseHelper->getNewYearBallToDate() ?? '')));

The str_replace() function in PHP 8.1 is deprecated for passing null to non-nullable internal function parameters. This means that if you use string casting, null will always be converted to an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.