8 PHP script skills (3)

Source: Internet
Author: User
Include files if the current Web site development project is large, 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, for example, if you have 1000 Web pages, even if you modify the footer that contains the copyright information only once a year, it will hurt you. SyntaxHighlighter. all ();

Include files

If you are currently working on a large Web site development project, 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, for example, if you have 1000 Web pages, even if you modify the footer that contains the copyright information only 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.

The main 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 range 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 are similar, but the 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 replacement process occurs when the PHP engine compiles your code instead of executing the code phase, which is different from include (). The latter is first computed 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 depend on 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]


 

 

 

 

 

 

 

 

 

 

 

 

 

 

You will certainly 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 certainly don't want these things to be part of the document as well.

You can place the included files (encoded or required) anywhere in the system, as long as the PHP you can use can access these files. You can also give these files any file extension you want to give, or do not have an extension.

Using the include () and require () functions makes it easier to process elements that are common or frequently changed on the Web site.


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.