PHP str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Asked Answered
S

8

16

I'm getting this error while running my app:

PHP str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

I am Using CodeIgniter Version : V4.1.8 PHP Version : 8.1.2, full error output below:

{"data":[
    ["omron","<span class=\"label label-success\">Active<\/span>",
    "<button type=\"button\" class=\"btn btn-default\" onclick=\"editBrand(4)\" data-toggle=\"modal\" data-target=\"#editBrandModal\"><i class=\"fa fa-pencil\"><\/i><\/button> <button type=\"button\" class=\"btn btn-default\" onclick=\"removeBrand(4)\" data-toggle=\"modal\" data-target=\"#removeBrandModal\"><i class=\"fa fa-trash\"><\/i><\/button>\n\t\t\t\t"]
    ]
}
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: 8192</p>
<p>Message:  str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated</p>
<p>Filename: core/Output.php</p>
<p>Line Number: 457</p>


    <p>Backtrace:</p>

Code

if ($this->parse_exec_vars === TRUE) 
{ 
    $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; 
    // below is line 457
    $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); 
}
Shulock answered 2/2, 2022 at 11:28 Comment(3)
Also as core/Output.php sounds like codeigniter code, look back up the stack trace to see what piece of your code called this code code and show us that. That is probably where the issue occursFrasco
I have updated this, still that showing the same ?Shulock
OK, so now show us whats in $output please. A var_dump($output); And all the other relevant variablesFrasco
L
5

This error message can show up if you have not loaded any views. Codeigniter's internal output buffer is never initialized thus being null. The output buffer is the third parameter to str_replace(). There might be other ways to trigger this error message.

You probably want to load a valid view, at some point.

PHP 7 and lower just would ignore the missing parameter, while PHP 8+ displays the warning. It may also vary with your environment / debug settings.

Lovell answered 8/2, 2022 at 10:14 Comment(0)
P
33

Using a ternary operator will fix this error.

Before Codeigniter existing code:

$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);

After Using Ternary Operator:

$output = $output ? str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output): "";
Puma answered 12/2, 2022 at 19:41 Comment(4)
Error in codeigniter's Output.php file (system/core/Output.php) You need to replace $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); This with $output = $output ? str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output): ""; . It is at line no. 457 in system/core/Output.phpBalance
for CI 3.x projects: this is fixed with the 3.1.13 release: either upgrade the whole installation or just replace the system/core/Output.php file, depending on your needs....Alembic
Altering System / Framework code should really be an ultra-last resort, especially since your patch will likely be overwritten with any framework update.Titan
Rather than the embedding the ternary operator in the call, I would do this above that line. I think it's clearer: $output = $output === null ? '' : $output;Kanchenjunga
L
5

This error message can show up if you have not loaded any views. Codeigniter's internal output buffer is never initialized thus being null. The output buffer is the third parameter to str_replace(). There might be other ways to trigger this error message.

You probably want to load a valid view, at some point.

PHP 7 and lower just would ignore the missing parameter, while PHP 8+ displays the warning. It may also vary with your environment / debug settings.

Lovell answered 8/2, 2022 at 10:14 Comment(0)
R
3

I think I found the issue. It is not a direct problem from str_replace() function, codeigniter written this perfectly, but you are using strip_quotes() function somewhere, and you are passing directly something to it like:

$d= strip_quotes($this->input->post('thusnder'));

If it is, then you must update your code by adding this:

$d= strip_quotes($this->input->post('thusnder') ?? '');

This will prevent data to be null and then it will not be passed to the strip_quotes() function which uses str_replace().

Rubio answered 5/1, 2023 at 0:8 Comment(0)
K
1

I also faced same issue just used these lines in your Controller constructor method

 public function __construct()
{
    parent::__construct();
    error_reporting(0);
    $this->load->library("session");
    $this->load->helper('url');
}
Klee answered 8/6, 2022 at 6:18 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Santanasantayana
T
1

As for as this error is Deprecated, so can't effect on your website behavior. You can disable PHP Deprecated warnings in your host manager (cpanel):

  1. Log into cPanel.
  2. Go to Home » Software » MultiPHP INI Editor.
  3. Click the "Editor Mode" tab.
  4. Choose intended domain from the dropdown.
  5. Find and edit/replace (or add if there is not) the "error_reporting" section using the following line :
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
  1. save
Thetis answered 16/5, 2023 at 16:26 Comment(1)
Good programmers don't hide problems, they fix them.Aldus
A
0

Another option is to check $output in the existing if statement. That will skip the entire block of code that isn't needed when $output is NULL or empty.

if ($this->parse_exec_vars === TRUE && !empty($output)) 
{ 
    $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; 
    // below is line 457
    $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); 
}
Antimonic answered 15/4, 2022 at 19:17 Comment(0)
H
0

You can user this code to get rid of it:

$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), (string) $output );
Hexarchy answered 29/11, 2022 at 13:5 Comment(0)
S
-7

I Have downgraded my CI version to 3 and PHP verison to 7.3

Its working now

Shulock answered 4/2, 2022 at 7:5 Comment(1)
Downgrading is a workaround, at best. Better to find the actual problem and fix it.Titan

© 2022 - 2024 — McMap. All rights reserved.