Solve the problem of PHP include with parameters. php include can load an external PHP file whose parameter is a file name. since it is a file name, it cannot contain parameters. In php, if the include file must contain parameters, there is only one way, that is, this parameter is a full-path URL address. Example: includelocalhostaaa. php ?; Correct inclu solves the include parameter problem in PHP
Php include can load an external php file. its parameter is a file name. since it is a file name, it cannot contain parameters.
In php, if the include file must contain parameters, there is only one way, that is, this parameter is a full-path URL address.
For example:
Include "http: // localhost/aaa. php? Id = 1 "; // correct
Include "aaa. php? Id = 1 "; // error
In fact, the include definition is only used to load an external file to the current file and does not explain the code. Therefore, it seems meaningless to include parameters.
For example, the following code (1.php ):
$ A = "123 ";
Include "aaa. php ";
To use aaa. php to receive the value of variable $ a, you only need to directly call aaa. php. for example, the code of aaa. php is as follows:
Echo $;
If you run 1.php in this way, the value of $ a will be output: 123.
However, if there is a problem, there must be a need. since we need to include parameters, it is certainly not just that we want to use the variables in the loader file in the loaded file.
The requirements are as follows:
You want to include different code segments in the same file based on different parameters.
Based on the above requirements, my solution is:
Write the code in the loaded File (assuming aaa. php) as a function, for example:
Function abc ($ x)
{
$ ReturnValue;
Switch ($ x)
{
Case 1:
$ ReturnValue = "apple"; break;
Case 2:
$ ReturnValue = "banana"; break;
Case 3:
$ ReturnValue = "carambola"; break;
}
}
Then write in the main file (suppose it is 1.php) as follows:
Include "aaa. php ";
Echo abc (1); // output: Apple
Echo abc (2); // output: Banana
Echo abc (3); // output: carambola
In fact, according to the above method, there is no big difference between directly using the variables in the main file, but the advantage of this is that no matter how the variables in the main file change, aaa. this function in php does not need to be modified and can be used in other files.
In this way, the difference between directly using the variables in the main file and calling the functions in the main file is obvious!
Original: Qian Xue network http://www.ayinsky.com
The following is a summary of some of my experiences: using readfile ("http: // XXXX/example. php? Name = valude "); you can pass a file with parameters ~~