将 null 传递给 string 类型的参数 #1 ($string) 已被弃用

问题描述 投票:0回答:1

切换到 PHP8.1 后,以下三个消息在我耳边飞扬:

`已弃用:strlen():在 /var/www/html/fp-plugins/bbcode/inc/stringparser.class.php 第 301 行中,已弃用将 null 传递给 string 类型的参数 #1 ($string)

    function parse($text) {
        if ($this->_parsing) {
            return false;
        }
        $this->_parsing = true;
        $this->_text = $this->_applyPrefilters($text);
        $this->_output = null;
        $this->_length = strlen($this->_text); // Line 301
        $this->_cpos = 0;
        unset($this->_stack);
        $this->_stack = array();
        if (is_object($this->_root)) {
            StringParser_Node::destroyNode($this->_root);
        }

我尝试用trim来转换参数

致以诚挚的问候

php strlen
1个回答
0
投票

错误消息表明

null
正在传递给
strlen()
函数,该函数在 PHP 8.1 中已弃用。
strlen()
函数需要一个字符串作为参数。

要解决此问题,您可以在将

$this->_text
传递给
null
之前确保它不是
strlen()
。如果
$this->_text
null
,可以将其转换为空字符串。具体方法如下:

$this->_text = $this->_applyPrefilters($text);
$this->_output = null;
$this->_text = $this->_text ?? ''; // Ensure that $this->_text is not null
$this->_length = strlen($this->_text); // Line 301

在此代码中,空合并运算符

??
用于检查
$this->_text
是否为
null
。如果是,它将被替换为空字符串
''
。这可确保
strlen()
始终接收字符串。

© www.soinside.com 2019 - 2024. All rights reserved.