Where are the files that were included or required from?

Source: Internet
Author: User

Over PHP, you can use different functions to help you reuse code. The specific function you use depends on what you intend to reuse.

The main functions are as follows:

* Include () and include_once ()

* Require () and require_once ()

The Include () function contains the given file and checks it. Such as:

Include ('/home/me/myfile ');

Any code in the contained file will be executed within the scope of the variable, which is equivalent to where the function include () appears in the parent code. You can use the include () and fopen () functions together to include static files on the server or target files on another server.

The include_once () function accomplishes the same function as the include () function, checking that only the code within the file has been included in the current script. If the code is already included in the script, the function will no longer contain the corresponding file.

The Require () function replaces itself with the contents of a given file. This substitution process occurs when the PHP engine compiles your code rather than the execution phase, which differs from the include (), which is computed first. The Require () function is more used for static elements, while the include () function is primarily used for dynamic elements. Similar to Include_once (), the require_once () function checks that the given code has already been inserted into the block, and if the code already exists, the given code will not be inserted again in the document.

It is recommended that the Require function be used for information such as copyrights, static text, and other elements that do not contain variables, and that elements that rely on other scripts to implement their own content should also take the Require function, for example:

<HTML>

<HEAD><TITLE>Something</TITLE></HEAD>

<BODY>

[A lot of content]

<?

Insert Copyright information

Require ('/home/me/mycopyright ');

?>

</BODY>

</HTML>
On the other hand, you often use the Include (2881064151) function to place libraries or similar libraries outside of a script:



<?

get my library of functions

Include ('/home/me/myfunctions ');

Use my functions to execute PHP programs? >

<HTML>

<HEAD><TITLE>Something</TITLE></HEAD>

<BODY>

[A lot of content]

</BODY>

</HTML>

The next natural question is: "Where are the files that were included or required from?" "The simple answer is:" Your system. "However, just a few snippets sometimes contain security information such as a database connection with a user name and password. Obviously you don't want these things to become part of the document and be made public.

You can put the included files (included or required) anywhere on the system, as long as the user's PHP can access the files. You can also give these files any extensions you want, or no extensions at all.

Use the include () and require () functions to externalize elements that are ubiquitous or frequently changing on a Web site. This approach also makes the system architecture easier to handle when upgrades are needed.

Where are the files that were included or required from?

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.