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

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


Front.inc:

$name = 'james';

返回文章页面

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

输出: James


完:

echo $name;

输出: 无


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

我用的是 Apache 服务器。如何配置它,以便不需要将 $name 声明为全局的?这两者之间是否存在利弊关系?

204957 次浏览

The parent file has access to variables in both included files

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.

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 front.inc1 of the file.

The included file doesn't have access to the other included file

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.

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

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.

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();

This is all you have to do:

In front.inc

global $name;
$name = 'james';

Same here with php 5.4.7

$mysql = false;
include("inc_mysql.php");
echo($mysql);

Where inc_mysql.php:

$mysql = true;

Echoes a value of $mysql = false;

BUT

if you remove the first $mysql=false;

include("inc_mysql.php");
echo($mysql);

Echoes a value of $mysql = true;

SO definitively, doing an include IS NOT like copying/pasting code, at least, in some PHP versions.

It's all about scope. It seems that in PHP the variables defined in the global scope are not automatically inherited in the function's scope. That's why, within a function body, you have to prepend the variable with the global keyword.

From the PHP manual:

A true global variable imported inside a function scope with the global statement actually creates a reference to the global variable.

So the variable $name is available to both includes (front.inc and end.inc ) but any function defined in those files would not have access to variable $name unless the global keyword is used or the $GLOBALS constant.

I've grabbed some code from https://www.php.net/manual/en/language.variables.scope.php to better understand this issue:

<?php
$a = 1; /* global scope */


function test()
{
/* the code below will throw a Warning: Undefined variable $a ... */
echo $a; /* reference to local scope variable */
}

To get the variable value you'll have to call global $a like this:

<?php
$a = 2; /* global scope */


function test2() {
global $a;
/* now we have access to the global $a so no more warnings */
echo $a;
}

You can also rely on $GLOBALS to get access to the global like this:

<?php
$a = 3; /* global scope */


function test3() {
echo $GLOBALS['a'];
}

You can get a list of all global variables with a code like this:

echo "<pre>";
print_r($GLOBALS);
echo "</pre>";

Source for code above: Is it possible to List Out All the Global variables in PHP?