本文介绍了无法在WordPress中访问已声明的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$g_value = 'something';
print "$g_value";

function get_value() {

    global $g_value;
    print $g_value;
}

print get_value();

当我在一个独立的PHP脚本中运行它时,我会得到'somethingsomething'。然而,当我在WordPress插件中运行它时,我只会得到'某些东西',全局声明并不会使该函数中的var变为可访问。我认为这应该始终工作,并且不依赖于register_globals或任何其他环境设置。这里发生了什么?

When I run it in a stand-alone PHP script, I get 'somethingsomething'. However, when I run it in a WordPress plugin, I only get 'something'- the global declaration does not make the var accessible in the function. I thought this should always work, and isn't dependent on register_globals or any other environment setting. What's going on here?

推荐答案

global $g_value;  //declare it global even before assigning it., this should fix it.

$g_value = 'something';
print "$g_value";

function get_value() {

    global $g_value;
    print $g_value;
}

print get_value();

这篇关于无法在WordPress中访问已声明的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:39