10 Tips for PHP Scripting (3)_PHP Tutorial

WBOY
Release: 2016-07-21 16:05:47
Original
809 people have browsed it

Included 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 , even if you only modify the footer containing copyright information once a year, it will make you miserable.

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()

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

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

Any code in the included 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 content, for example:


Something

[a lot of content]

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


< /HTML>

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 include databases with usernames and passwords. Security information such as connection functions, in this case, obviously, you definitely don't want these things to become part of the document and make the whole world know.

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/315589.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,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template