While upgrading to Magento 2.4.6, you may face this error often.
Exception #0 (Exception): Deprecated Functionality: strip_tags(): Passing null to parameter #1 ($string) of type string is deprecated.
From PHP 8.1, some functions like strip_tags(), htmlspecialchars(), mb_strtolower(), and trim() functions throw errors when you try to pass a null value to the function’s parameters.
The quick fix in most cases is to use the null coalescing operator to provide a default value as appropriate, so you don’t need a long null check around every use. For instance, htmlspecialchars($something) can be replaced with htmlspecialchars($mystring ?? ”)
Depending on your case, you may be able to just fix them manually a few at a time, either adding ?? ” or fixing a logic bug where you weren’t expecting a null anyway.
Examples :
<?php
//1) for strip_tags()
public function getPrintableOptionValue($optionValue)
{
return strip_tags($optionValue ?? '');
}
//2) for mb_strtolower()
return mb_strtolower((string) $value);
//3) for htmlspecialchars()
$href = htmlspecialchars($member['website'] ?? '');
//4) for trim()
return trim($value ?? '')
Let me know any other functions that caused issues in upgrading your project.
Happy Upgrading 🙂
You may also like
How to Use ViewModel in Magento 2: A Simple Guide
Magento 2: How to use helper in PHTML?
Leave a Comment