变量未转移到不同目录中的包含文件
所以,我有一个 WordPress 主题。
在functions.php文件中,我正在编写一个看起来像这样的函数:
function my_function(){
$var1 = 'apple';
$var2 = 'orange';
include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}
fruits.php文件看起来像这样:
<?php
echo "My brother's favorite fruit is an $var1, but my favorite fruit is an $var2.";
?>
当我在主题中的某处调用my_function()时,fruits.php中的echo语句显示在屏幕上,但不显示 $var1 和 $var2 的值。它们只是空白。
现在这是奇怪的部分。如果我将fruits.php移至与functions.php相同的目录,并将my_function()中的include语句更改为:
include 'fruits.php';
这2个变量显示得很好。
有什么想法可能导致这个问题吗?
仅供参考,我确实尝试在变量中定义fruits.php 的路径,然后尝试
include $path;
无济于事。
So, I have a Wordpress theme.
In the functions.php file, I'm writing a function that looks something like this:
function my_function(){
$var1 = 'apple';
$var2 = 'orange';
include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}
And the fruits.php file looks like this:
<?php
echo "My brother's favorite fruit is an $var1, but my favorite fruit is an $var2.";
?>
When I call my_function() somewhere in my theme, the echo statement in fruits.php displays on the screen, but the values of $var1 and $var2 do not display. They're simply blank.
Now here's the strange part. If I move fruits.php to the same directory as functions.php, and change the include statement in my_function() to this:
include 'fruits.php';
the 2 variables display just fine.
Any ideas what could be causing this problem?
FYI, I did try defining the path to fruits.php in a variable and then trying
include $path;
to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/language.variables.scope.php
您在包含目录前加上 wordpress
stylesheet_directory
(而不是例如STYLESHEETPATH
常量),这是一个 URL,而不是文件路径,因此不应将其用于包含PHP 中的文件。更改此设置以正确包含该文件。You're prepending the include directory with wordpress
stylesheet_directory
(rather thanSTYLESHEETPATH
constant for example) which is a URL, not a file path so it shouldn't be used to include files in PHP. Change this to correctly include the file.这里存在一个与函数中变量的范围有关的问题。基本上,您有一个文件,例如
index.php
,其中包含来自其他目录的functions.php
。在functions.php
中,您在函数作用域内定义了两个变量 - 这意味着这些变量仅在函数内可见。此外,此函数还包括fruits.php
文件。由于
fruits.php
在技术上包含在index.php
中,因此它会查找变量$var1
和$var2
> 在index.php
中,它们尚未在那里定义,因为它们已在函数范围内定义。基本上它会寻找它看不到的变量。有两种方法可以解决此问题:
方法一:
将函数修改为以下内容。
通过这种方式,您可以强制变量在函数中不可见,而是全局的,或者换句话说,在整个文件中可见。
方法2:
在函数外部定义变量。
有关变量范围的更多信息,您可以访问此处 http://php.net/ Manual/en/language.variables.scope.php
希望这会有所帮助。
There is an issue here with the scope of the variables in the function. Basically you have a file, lets say
index.php
within which you include thefunctions.php
from some other directory. In thatfunctions.php
you define two variables within a function scope - meaning the variables are only visible within the function. In addition this function includes thefruits.php
file.Since
fruits.php
is technically included by theindex.php
, it looks for the variables$var1
and$var2
withinindex.php
and they have not been defined there because they have been defined within a scope of the function. Basically it looks for a variables which it cannot see.Here are two ways to solve this:
Method 1:
Modify the function to the following.
This way you are forcing the variables not to be visible within the function, but to be global, or in other words to be visible within the whole file.
Method 2:
Define the variables outside of the function.
For more information regarding variable scope, you can go here http://php.net/manual/en/language.variables.scope.php
Hope this helps.