Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress是否清除$GLOBALS?_Php_Wordpress_Global Variables - Fatal编程技术网

Php WordPress是否清除$GLOBALS?

Php WordPress是否清除$GLOBALS?,php,wordpress,global-variables,Php,Wordpress,Global Variables,我想做的是在wordpress主题中包含一个PHP脚本。问题是,在包含脚本文件后,我无法访问主题文件中的函数,脚本文件中声明的变量 我在theme文件夹中创建了一个新文件,并添加了与header.php中相同的代码,如果我打开该文件,它就可以正常工作。据我所知,这与文字出版有关 /other/path/wordpress/wp-content/themes/theme-name/header.php // this is broken /other/path/wordpress/wp-conte

我想做的是在wordpress主题中包含一个PHP脚本。问题是,在包含脚本文件后,我无法访问主题文件中的函数,脚本文件中声明的变量

我在theme文件夹中创建了一个新文件,并添加了与header.php中相同的代码,如果我打开该文件,它就可以正常工作。据我所知,这与文字出版有关

/other/path/wordpress/wp-content/themes/theme-name/header.php // this is broken
/other/path/wordpress/wp-content/themes/theme-name/test.php   // this works

/var/www/vhosts/domain/wordpress/ ->(symlink)-> /other/path/wordpress/
                                                /other/path/wordpress/wp-content/themes/theme-name/header.php
/var/www/vhosts/domain/include_file.php
内容:/var/www/vhosts/domain/include_file.php

$global_var = 'global';
print_r($GLOBALS);  // if I open this file directly this prints globals WITH $global_var;
                    // if this file is included in header this prints all the WP stuff WITHOUT $global_var;
内容:/other/path/wordpress/wp Content/themes/theme name/header.php 需要“/path/to/include_file.php”

print $global_var; // this prints 'global' as expected
function test()
{
    global $global_var;
    print $global_var; // this is NULL
}
test();
print_r($GLOBALS); // this prints all the WP stuff WITHOUT $global_var in it

我不是在推广使用
$GLOBALS
,但无论如何。。。使用以下命令定义变量:

$GLOBALS['varname'] = 'value';

这应该行得通。我怀疑你实际上并不像你想象的那样在全球范围内。该文件可能被函数包含,在这种情况下,您就在函数范围内。

我不是在推广使用
$GLOBALS
,但无论如何。。。使用以下命令定义变量:

$GLOBALS['varname'] = 'value';

这应该行得通。我怀疑你实际上并不像你想象的那样在全球范围内。该文件可能被函数包含,在这种情况下,您就在函数范围内。

全局文件是不好的做法,不要使用itI。我需要将其用于充当自动加载程序并自动接收其参数的函数。全局是获取数据的最简单方法。全局是不好的做法,不要使用itI。我需要将其用于充当自动加载程序并自动接收其参数的函数。全局是获取数据的最简单方法。我刚刚找到了答案,然后返回发布答案。但你是对的,我不在正确的范围内。谢谢我刚刚弄明白了,然后返回发布答案。但你是对的,我不在正确的范围内。谢谢