常见的php变量转字符串

最近抽风了要用php8结果好多函数严格性的要求都开始噼里啪啦的报错整的头大。
今天在开发的过程中遇到了一个报错如下
程序运行异常: preg_match(): Argument #2 ($subject) must be of type string, stdClass given
我本身的程序写了一个递归替换如下

// 递归替换
function preg_replace_r($search, $replace, $subject)
{
    while (preg_match($search, $subject)) {
        $subject = preg_replace($search, $replace, $subject);
    }
    return $subject;
}

在php7的版本中无事php8报错所以干脆写了一个转换函数

function diy_to_string($variable)
{
return is_float($variable)?(string)$variable:(is_resource($variable)?"'resource of type'":var_export($variable, true));
}

所以我们就直接替换一下就行了
preg_match($search, diy_to_string($subject))
就先这样吧,接着处理问题去。

你可能感兴趣的:(常见的php变量转字符串)