Solve the include parameter-related problems in PHP.

Source: Internet
Author: User
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 ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.