函数内部不存在全局变量值

时间:2017-11-20 20:10:32

标签: php

我有以下功能使用PHP在图像中创建和显示填充矩形:

$GLOBAL['feed_background'] = imagecreatefromjpeg('images/feed_background.jpg');

function test() {
    $white = imagecolorallocate($GLOBAL['feed_background'], 255, 255, 255);
    imagefilledrectangle($GLOBAL['feed_background'], 10, 10, 50, 50, $white);
}

出于某种原因,这种方式提及$GLOBAL变量不起作用,并且矩形不会出现。

这样做会起作用:

$feed_background = imagecreatefromjpeg('images/feed_background.jpg');

function test() {
    global $feed_background;
    $white = imagecolorallocate($feed_background, 255, 255, 255);
    imagefilledrectangle($feed_background, 10, 10, 50, 50, $white);
}

第一块代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

我的代码中出现语法错误。 $GLOBAL应为$GLOBALS。愚蠢的我!

相关问题