When I put include into a function, the variable contained cannot be called? 1. phpecho & nbsp; $ a; 2.php$ a & nbsp; aaaaaaaaaaaa; include (1.php); write and execute 2. php can get the output correctly, but below is not 2. phpfunction & nbsp; loadFile ($ filename) {& the variables included in the include statement cannot be called?
1. php
echo $a;
2. php
$a = "aaaaaaaaaaaa";
include("1.php");
In this way, write and execute 2. php to get the output correctly, but below is not possible
2. php
function loadFile($filename){
include $filename;
}
$a = "aaaaaaaaaaaa";
loadFile("1.php");
Why? Put include into the function. the included files cannot reference their variables.
------ Solution --------------------
function loadFile(){
echo $a;
}
$a = "aaaaaaaaaaaa";
loadFile();
You can try it.
------ Solution --------------------
Variable scope issues.
If you include 1.php into the function, the variables declared outside the function cannot work for it. Unless you perform a global operation in the function. Or use $ GLOBALS ['A'] in 1. php to replace $.
In addition, I have a colleague who also likes to write a lot of files and include them everywhere,
I am very resistant to this practice, which causes global variable pollution. it is often difficult to find a variable to declare or declare a variable, for fear of conflict. And the code is messy.
We recommend that you do not use this method. It is recommended that variables be controlled in a local place, which not only improves the performance of php, but also makes the code logic clearer.
------ Solution --------------------
Okay, this is a scope issue.