Dynamic web skills PHP programs contain file titles

Source: Internet
Author: User
If you are currently working on a large scope of web site development projects, you must have a deep understanding of code reuse. in a metaphor, whether it is html or php code block, as long as the project is large enough, for example, if you have 1000 Web pages, you can only modify the copyright once a year.

If you are currently working on a large scope of web site development projects, you must have a deep understanding of code reuse. in a metaphor, whether it is html or php code block, as long as the project is large enough, for example, if you have 1000 Web pages, even if you only modify the footer containing the copyright information once a year, it will hurt you.

Php can use some functions to help you reuse code. the specific functions you need depend on the content you intend to reuse.

Important functions are:

* Include () and include_once ()
* Require () and require_once ()

The include () function contains the specified file, for example:

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

Any code in the include file will be executed within the variable category of the code page where include () is located. You can include static files on the server or target files on other servers by combining include () and fopen ().

The include_once () and include () functions have similar functions, but this function checks whether the code in the included file has been included in the current script. If the code has been included by the script, the function will no longer contain the corresponding files.

The require () function replaces itself with the content of the given file. This swap process is generated when the php engine compiles your code instead of performing the code phase. this is different from include (). The latter is first calculated and then added to the document block. The require () function is mostly used for static elements, while the include () function is mainly used for dynamic elements. Similar to include_once (), the require_once () function checks whether the given code has been inserted into the document. if so, the given code will not be inserted into the document again.

I recommend that you use the require function for information such as copyright, static text, and other elements without Variables. The require function is also recommended for elements that are attached to other scripts to implement their own content. for example:


Something

[A lot of content]
// Insert copyright
Require ('/home/me/mycopyright ');
?>

On the other hand, I often use the include () function to put the function library or similar content out of the script:

// Get my function library
Include ('/home/me/myfunctions ');
// Do php things with my functions?>

Something

[A lot of content]

Are you sure you want to ask: "So, where are the files that are encoded or required from ?" The answer is simple: "your system ." However, sometimes some codes contain security information such as database connection functions with user names and passwords. in such cases, it is obvious that, you are sure you don't want to read these things and become part of the document.

You can place the included files (encoded or required) in any place of the system, as long as the php you can use can visit these files. You can also give these files an extended name for any file you want to see, or do not have an extended name.

Using the include () and require () functions makes it easier to process elements that are widely present on websites or that are frequently changed.


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.