在运行我的应用程序时,我收到了这个错误:
PHP str_replace():不推荐将null传递给类型数组的参数#3 ($subject)
我使用的是CodeIgniter版本:V4.1.8PHP版本: 8.1.2,下面是完全错误输出:
{"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>
更新:代码
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);
}
发布于 2022-02-08 10:14:25
如果没有加载任何视图,则会显示此错误消息。Codeigniter的内部输出缓冲区从未初始化过,因此是null
。输出缓冲区是str_replace()
的第三个参数。可能还有其他方法触发此错误消息。
您可能希望在某个时候加载一个有效的视图。
PHP 7和更低的版本只会忽略缺少的参数,而PHP 8+则会显示警告。它也可能随您的环境/调试设置而变化。
发布于 2022-02-12 19:41:37
使用三元操作符的将修复此错误。
Codeigniter现有代码之前的:
$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
希望它能很好地工作,因为我们将将str_replace转义为一个空值。
使用三元运算符后的:
$output = $output ? str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output): "";
发布于 2022-06-08 06:18:01
我还遇到了同样的问题--在Controller构造函数方法中使用了这些行
public function __construct()
{
parent::__construct();
error_reporting(0);
$this->load->library("session");
$this->load->helper('url');
}
https://stackoverflow.com/questions/70954797
复制相似问题