If you are currently working on a larger web site development project, then you must have a deep understanding of code reuse, for example, whether it is HTML or PHP code block, as long as the project is large enough, such as 1000 Web pages, you even a year to modify the page contains copyright information will be painful.
PHP can use some functions to help you reuse your code, and the function you are using depends on what you intend to reuse.
The main functions are:
* Include () and include_once ()
* Require () and require_once ()
The Include () function contains the given file, such as:
Include ('/home/me/myfile ');
Any code in the containing file will be executed within the variable scope of the code page that contains (). You can include static files on the server or target files that are contained on other servers by combining include () and fopen ().
The include_once () function is similar to the Include () function, except that the function checks whether the code within the included file has been included in the current script. If the code is already contained by the script, the function no longer contains 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 instead of executing the code phase, which is not the same as include (), which is first computed and then added to the text block. The Require () function is used mostly for static elements, while the include () function is used primarily for dynamic elements. Similar to Include_once (), the require_once () function checks whether a given code has been inserted into the document for a long time, and if so, the given code will not be inserted again.
I recommend that you use the Require function 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 are best to use the Require function, for example:
<HTML>
<HEAD><TITLE>Something</TITLE></HEAD>
<BODY>
[A lot of content]
On the other hand, I often use the include () function to put library or similar content outside of the script:
?
get my function library
Include ('/home/me/myfunctions ');
Do PHP things and my functions?>
<HTML>
<HEAD><TITLE>Something</TITLE></HEAD>
<BODY>
[A lot of content]
</BODY>
</HTML>
You are sure to ask, "So where are the documents that were included or required?" "The answer is simple:" Your system. "However, sometimes, some code contains security information such as a database connection function with a username and password, and in this case, it's obvious that you don't want these things to be part of the documentation that the world knows."
You can place the containing file (included or required) anywhere in the system, as long as the user can use PHP to access the files. You can also give these files any file extensions you wish to give, or no extensions.
Use the include () and require () functions to make elements that are ubiquitous or frequently changed on your Web site easier to handle.
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.