json_decode (depreciation)

ignite

New Member
License Active
Notice that PHP locally on both 8.2 and even 8.3 this is popping out:

Issue:
Deprecated
: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in C:\laragon\www\topsite.test\index.php on line 256

Line/Code:

PHP:
$CONF['categories'][$category]['old_slugs'] = (array) json_decode($old_slugs, true);
 

Mark

Administrator
Staff member
The deprecation warning you're seeing occurs because json_decode expects a string as its first argument, but in your code, $old_slugs is null. To fix this issue, you should add a check to ensure that $old_slugs is not null before passing it to json_decode.


Here’s how you can modify the line:

Code:
$CONF['categories'][$category]['old_slugs'] = (array) ($old_slugs !== null ? json_decode($old_slugs, true) : []);

This code checks if $old_slugs is not null. If it's not null, it decodes the JSON string. If it is null, it assigns an empty array to $CONF['categories'][$category]['old_slugs'].


Alternatively, you can use a more explicit check and provide a default value:

Code:
if ($old_slugs !== null) {
    $CONF['categories'][$category]['old_slugs'] = (array) json_decode($old_slugs, true);
} else {
    $CONF['categories'][$category]['old_slugs'] = [];
}
 
Top