将一个变量从一个php包含文件传递到另一个:global vs. not

时间:2022-12-18 00:05:49

I'm trying to pass a variable from one include file to another. This is NOT working unless I declare the variable as global in the second include file. However, I do NOT need to declare it as global in the file that is calling the first include. For example:

我试图将一个包含文件的变量传递给另一个包含文件。除非我在第二个include文件中声明该变量为全局变量,否则这将不起作用。但是,我不需要在调用第一个include的文件中声明它为全局的。例如:


front.inc:

front.inc:

$name = 'james';

index.php:

index . php:

include('front.inc');
echo $name;
include('end.inc');

output: james

输出:詹姆斯


end.inc:

end.inc:

echo $name;

output: nothing

输出:无


IF I declare global $name prior to echoing $name in end.inc, then it works properly. The accepted answer to this post explains that this depends on your server configuration: Passing variables in PHP from one file to another

如果我在最后回显$name之前声明全局$name。然后它就能正常工作了。这篇文章的公认答案解释了这取决于您的服务器配置:将PHP中的变量从一个文件传递到另一个文件

I'm using an Apache server. How would I configure it so that declaring $name to be global is not necessary? Are there advantages/disadvantages to one vs. the other?

我使用的是Apache服务器。我将如何配置它,这样就不需要声明$name为全局变量?一个人对另一个人有利/不利吗?

4 个解决方案

#1


54  

When including files in PHP, it acts like the code exists within the file they are being included from. Imagine copy and pasting the code from within each of your included files directly into your index.php. That is how PHP works with includes.

当在PHP中包含文件时,它的行为就像代码存在于包含它们的文件中一样。想象一下,将包含的每个文件中的代码直接复制并粘贴到index.php中。这就是PHP如何处理include的。

So, in your example, since you've set a variable called $name in your front.inc file, and then included both front.inc and end.inc in your index.php, you will be able to echo the variable $name anywhere after the include of front.inc within your index.php. Again, PHP processes your index.php as if the code from the two files you are including are part of the file.

在你的例子中,因为你在前面设置了一个名为$name的变量。inc文件,然后包括两个正面。公司和结尾。公司在你的索引。php,您将能够在包含front之后的任何地方回显变量$name。公司在您的index . php。同样,PHP处理索引。php就像您所包含的两个文件中的代码是文件的一部分一样。

When you place an echo within an included file, to a variable that is not defined within itself, you're not going to get a result because it is treated separately then any other included file.

当您将一个echo放置在一个包含的文件中,到一个本身没有定义的变量中时,您将不会得到一个结果,因为它与任何其他包含的文件都是分开处理的。

In other words, to do the behavior you're expecting, you will need to define it as a global.

换句话说,要执行预期的行为,需要将其定义为全局变量。

#2


25  

Here is a pitfall to avoid. In case you need to access your variable $name within a function, you need to say "global $name;" at the beginning of that function. You need to repeat this for each function in the same file.

这里有个陷阱要避免。如果需要在函数中访问变量$name,则需要在函数的开头写上“global $name”。您需要对同一文件中的每个函数重复此操作。

include('front.inc');
global $name;

function foo() {
  echo $name;
}

function bar() {
  echo $name;
}

foo();
bar();

will only show errors. The correct way to do that would be:

只会显示错误。正确的做法是:

include('front.inc');

function foo() {
  global $name;
  echo $name;
}

function bar() {
  global $name;
  echo $name;
}

foo();
bar();

#3


-1  

This is all you have to do:

这就是你要做的:

In front.inc

在front.inc

global $name;
$name = 'james';

#4


-10  

I have a strange solution to that. in the file end.inc, add this line:

我有一个奇怪的解决办法。在文件结束。公司,添加这一行:

$name=$name;

Then the echo would work.

然后回声就会工作。

I hit into this solution in my project, without a good explanation why it worked like this.

在我的项目中,我遇到了这个解决方案,却没有很好的解释为什么它会这样工作。

#1


54  

When including files in PHP, it acts like the code exists within the file they are being included from. Imagine copy and pasting the code from within each of your included files directly into your index.php. That is how PHP works with includes.

当在PHP中包含文件时,它的行为就像代码存在于包含它们的文件中一样。想象一下,将包含的每个文件中的代码直接复制并粘贴到index.php中。这就是PHP如何处理include的。

So, in your example, since you've set a variable called $name in your front.inc file, and then included both front.inc and end.inc in your index.php, you will be able to echo the variable $name anywhere after the include of front.inc within your index.php. Again, PHP processes your index.php as if the code from the two files you are including are part of the file.

在你的例子中,因为你在前面设置了一个名为$name的变量。inc文件,然后包括两个正面。公司和结尾。公司在你的索引。php,您将能够在包含front之后的任何地方回显变量$name。公司在您的index . php。同样,PHP处理索引。php就像您所包含的两个文件中的代码是文件的一部分一样。

When you place an echo within an included file, to a variable that is not defined within itself, you're not going to get a result because it is treated separately then any other included file.

当您将一个echo放置在一个包含的文件中,到一个本身没有定义的变量中时,您将不会得到一个结果,因为它与任何其他包含的文件都是分开处理的。

In other words, to do the behavior you're expecting, you will need to define it as a global.

换句话说,要执行预期的行为,需要将其定义为全局变量。

#2


25  

Here is a pitfall to avoid. In case you need to access your variable $name within a function, you need to say "global $name;" at the beginning of that function. You need to repeat this for each function in the same file.

这里有个陷阱要避免。如果需要在函数中访问变量$name,则需要在函数的开头写上“global $name”。您需要对同一文件中的每个函数重复此操作。

include('front.inc');
global $name;

function foo() {
  echo $name;
}

function bar() {
  echo $name;
}

foo();
bar();

will only show errors. The correct way to do that would be:

只会显示错误。正确的做法是:

include('front.inc');

function foo() {
  global $name;
  echo $name;
}

function bar() {
  global $name;
  echo $name;
}

foo();
bar();

#3


-1  

This is all you have to do:

这就是你要做的:

In front.inc

在front.inc

global $name;
$name = 'james';

#4


-10  

I have a strange solution to that. in the file end.inc, add this line:

我有一个奇怪的解决办法。在文件结束。公司,添加这一行:

$name=$name;

Then the echo would work.

然后回声就会工作。

I hit into this solution in my project, without a good explanation why it worked like this.

在我的项目中,我遇到了这个解决方案,却没有很好的解释为什么它会这样工作。