变量未转移到不同目录中的包含文件

发布于 2024-09-29 11:50:15 字数 756 浏览 3 评论 0原文

所以,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里人 2024-10-06 11:50:15

您在包含目录前加上 wordpress stylesheet_directory (而不是例如 STYLESHEETPATH 常量),这是一个 URL,而不是文件路径,因此不应将其用于包含PHP 中的文件。更改此设置以正确包含该文件。

You're prepending the include directory with wordpress stylesheet_directory (rather than STYLESHEETPATH 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.

枕花眠 2024-10-06 11:50:15

这里存在一个与函数中变量的范围有关的问题。基本上,您有一个文件,例如 index.php,其中包含来自其他目录的 functions.php。在 functions.php 中,您在函数作用域内定义了两个变量 - 这意味着这些变量仅在函数内可见。此外,此函数还包括 fruits.php 文件。

由于 fruits.php 在技术上包含在 index.php 中,因此它会查找变量 $var1$var2 > 在 index.php 中,它们尚未在那里定义,因为它们已在函数范围内定义。基本上它会寻找它看不到的变量。

有两种方法可以解决此问题:

方法一:

将函数修改为以下内容。

function my_function(){
    global $var1 = 'apple';
    global $var2 = 'orange';
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

通过这种方式,您可以强制变量在函数中不可见,而是全局的,或者换句话说,在整个文件中可见。

方法2:

在函数外部定义变量。

$var1 = 'apple';
$var2 = 'orange';
function my_function(){
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

有关变量范围的更多信息,您可以访问此处 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 the functions.php from some other directory. In that functions.php you define two variables within a function scope - meaning the variables are only visible within the function. In addition this function includes the fruits.php file.

Since fruits.php is technically included by the index.php, it looks for the variables $var1 and $var2 within index.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.

function my_function(){
    global $var1 = 'apple';
    global $var2 = 'orange';
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

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.

$var1 = 'apple';
$var2 = 'orange';
function my_function(){
    include get_bloginfo('stylesheet_directory').'/lib/fruits.php';
}

For more information regarding variable scope, you can go here http://php.net/manual/en/language.variables.scope.php

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文