简体   繁体   English

PHP中的全局变量-Wordpress

[英]Global variables in PHP - Wordpress

So, I've got a variable that is set when an array returns a value that is identical to a strong. 因此,我有一个变量,该变量在数组返回与强数相同的值时设置。 it's the most complex thing I've dealt with. 这是我处理过的最复杂的事情。 Now, I've just got one more goal. 现在,我还有一个目标。

See, my site is a Wordpress site. 看,我的网站是一个Wordpress网站。 all these calculations are taking place in the header, but the code I want changed is in the footer. 所有这些计算都在页眉中进行,但我要更改的代码在页脚中。 The answer I seem to draw, is that I need to make the variable into a global variable. 我似乎得出的答案是,我需要使变量成为全局变量。 I've been trying for a few hours, but I can't seem to get it right. 我已经尝试了几个小时,但似乎无法正确解决。

Can someone tell me what I need to do? 有人可以告诉我我需要做什么吗? I've tried solutions seen in other questions on here, but no solutions seem to work for me. 我已经尝试过在其他问题上看到的解决方案,但似乎没有解决方案适合我。

here is the code for the header. 这是标题的代码。 With this site, it broaches a popular series' core communities. 通过此网站,它可以访问热门系列的核心社区。 To ensure the site remains largely undiscovered until launch, I've changed some URLs and IDs! 为了确保该网站在启动之前基本上未被发现,我更改了一些URL和ID!

$message_array = file("http://www.example.com/wp-content/themes/mytheme/Subtitles.css", FILE_IGNORE_NEW_LINES);

$message = array_rand($message_array);

$GfCheck = '<audio id="audio" src="http://www.example.com/wp-content/uploads/2016/01/example.wav" preload="auto" ></audio><a onclick="test()"><img src="http://example.com/wp-content/uploads/2016/01/Gf.png" height="90px" alt="test" title="test"/></a>';

if ($message_array[$message] == $GfCheck) {
$Gf = "1";
$GLOBALS['Gf']
}
else { }

echo "$message_array[$message]";

And here is the code I have in the footer 这是我在页脚中的代码

if($GLOBALS['Gf'])  {echo "Text to add to footer"; } ?>

It is worth noting, the second I add the $GLOBALS to the header, any tests I have in there (my test to make sure the code actually detected the correct line) cease functioning, indicating the variable stops working. 值得注意的是,第二次我将$ GLOBALS添加到标头中,在那里我进行的任何测试(我的测试以确保代码实际检测到正确的行)都停止运行,指示变量停止工作。 As such, the code as I've presented does not work, but if I remove the globals tag, it does work perfectly for the header alone. 因此,我提供的代码无法正常工作,但是如果删除globals标记,则仅对标头确实可以正常工作。 any idea what I can do? 知道我能做什么吗?

You're not handling these well. 您处理得不好。

$GLOBALS['Gf']

That's not even proper syntax. 那甚至不是正确的语法。 You need to set it to something at least. 您至少需要将其设置为某些值。 What I would do, instead of a $GLOBALS is to use a constant, which is global to all scopes 我要做的是使用一个常量,而不是$GLOBALS ,它对于所有作用域都是全局的

if ($message_array[$message] == $GfCheck) {
    $Gf = "1";
    define('GFCHECK', '1');
}

Then, later in your code 然后,在您的代码中

if(defined('GFCHECK'))  {echo "Text to add to footer"; } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM