PHP 错误注意:未定义的偏移量:,尝试访问 null 类型值的数组偏移量

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

我在服务器日志中收到多个 php 错误,该错误显示在我的 php 代码的 2 行上,但是 php 脚本正在运行,但它在服务器日志中显示这些错误。

PHP Notice:  Trying to access array offset on value of type null 
PHP Notice:  Undefined offset: 79
PHP Notice:  Undefined offset: 89
PHP Notice:  Undefined offset: 32 

这里我添加了显示错误的部分 php 代码:

function right2img($color,$text,$pos,$font,$fontxy,$im){
    $letters = imagecreatefromstring(base64_decode($font));
    $rgb = getRGB($color);
    $index = imagecolorexact($letters, 0, 0, 0);
    imagecolorset ($letters, $index, $rgb[0], $rgb[1], $rgb[2]);
    for($i=0;$i<strlen($text);$i++){
        $c = ord($text[$i]);
        imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
        $pos+= $fontxy[$c]["w"];
    }
    return $im;
}

错误来自上面这两行代码

    imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
    $pos+= $fontxy[$c]["w"];

不知道如何解决它。 ....有什么帮助吗?

php php-7 php-5.3 e-notices
1个回答
0
投票

$fontxy 可能为空。
您必须检查 $fontxy 的有效索引值。

if(isset($fontxy[$c]) && isset($fontxy[$c]["x"]) && isset($fontxy[$c]["y"])&& isset($fontxy[$c]["w"])){
imagecopy ($im, $letters, $pos, 5, $fontxy[$c]["x"], $fontxy[$c]["y"], $fontxy[$c]["w"], 5);
        $pos+= $fontxy[$c]["w"];
}
© www.soinside.com 2019 - 2024. All rights reserved.