Home >Backend Development >PHP Tutorial >8 Tips for PHP Scripting (3)_PHP Tutorial

8 Tips for PHP Scripting (3)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:21:09980browse

Include files 

If you are currently working on a large-scale Web site development project, then you must have a deep understanding of code reuse. For example, whether it is HTML or PHP code blocks, as long as the project is large enough, such as 1,000 Web pages, you Changing the footer containing copyright information even once a year can be a pain in the ass.

PHP can help you reuse code through some functions. The specific functions you want to use depend on the content you plan to reuse.

The main functions are:

* include() and include_once()
* require() and require_once()

The

include() function includes the given file, such as:

include(/home/me/myfile);

Any code in the include file will be executed within the variable scope of the code page where include() is located. You can include static files on the server or include object files on other servers by using include() and fopen() in combination.
The include_once() function is similar to the include() function, except that this function will check whether the code in the included file has been included by the current script. If the code is already included in the script, the function no longer includes the corresponding file.
The require() function replaces itself with the contents of the given file. This replacement process occurs when the PHP engine compiles your code rather than executing it, which is different from include(), which is first calculated and added to the document. 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 already been inserted into the document. If so, the given code will not be inserted into the document again.
I suggest you use the require function for information such as copyright, static text, and other elements that do not contain variables. It is also best to use the require function for elements that rely on other scripts to implement their own content, such as:


Something

[a lot of content]

// insert copyright
require(/home/me/mycopyright);
?>


On the other hand, I often use the include() function to put libraries or similar content outside the script:

// get my function library
include(/home/me/myfunctions);

// do PHP things with my functions ?>


Something

[a lot of content]


You will definitely ask: "So, where do the included or required files come from?" The answer is simple: "Your system." However, sometimes, some code will contain files such as files with user names and passwords. Security information such as database connection functions, in this case, obviously, you definitely don't want these things to become part of the document and become known to the whole world.

You can place included files (included or required) anywhere in the system, as long as the PHP available to users can access these files. You can also give these files any file extension you wish, or no extension at all.

Using the include() and require() functions can make elements that are common or frequently changing on the website easier to handle.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532526.htmlTechArticleIncluded files If you are currently working on a large-scale Web site development project, then you must have a deep understanding of code reuse , for example, whether it is HTML or PHP code blocks, as long as the project is large enough...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn