将变量从一个 php 包含文件传递到另一个:全局与非
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4675932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a variable from one php include file to another: global vs. not
提问by maxedison
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:
我试图将一个变量从一个包含文件传递到另一个。这不起作用,除非我在第二个包含文件中将该变量声明为全局变量。但是,我不需要在调用第一个包含的文件中将其声明为全局。例如:
front.inc:
前线公司:
$name = 'james';
index.php:
索引.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
如果我在 end.inc 中回显 $name 之前声明 global $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 声明为全局?一个相对于另一个有优势/劣势吗?
采纳答案by Michael Irigoyen
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 与包含一起工作的方式。
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 partof the file.
所以,在你的榜样,因为你已经设置了一个名为变量$name
在你的front.inc
文件,然后既包括front.inc
和end.inc
你的index.php
,你就可以echo
变$name
后的任何地方include
的front.inc
你内index.php
。同样,PHP 会处理您index.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.
当您将 an 放入echo
包含文件中时,将一个变量放在其本身未定义的变量中时,您将不会得到结果,因为它与任何其他包含文件分开处理。
In other words, to do the behavior you're expecting, you will need to define it as a global.
换句话说,要执行您期望的行为,您需要将其定义为全局。
回答by Ka?an Kayal
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();
回答by Paul Appleby
This is all you have to do:
这就是你所要做的:
In front.inc
在front.inc
global $name;
$name = 'james';