Home > Backend Development > PHP Tutorial > In-depth understanding of the differences between require, require_once, include and include_once_PHP Tutorial

In-depth understanding of the differences between require, require_once, include and include_once_PHP Tutorial

WBOY
Release: 2016-07-21 15:09:33
Original
935 people have browsed it

PHP has the characteristics of fast, reliable, cross-platform application, and open source code, making PHP one of the most popular server-side Script languages. Based on what I have experienced at work, I will introduce to you my experience in using PHP. I hope it will be helpful to everyone.

Use PHP's Include files to maintain your website
No matter the size of the website you develop is large or small, you should realize the importance of reusing program code , whether you are reusing PHP programs or HTML source code. For example, the copyright notice at the end of the website page must be revised at least once a year. What should you do if your website has many pages? It must be a headache to modify these pages one by one. With PHP we can reuse program code in several different ways. Which functions to use depends on what kind of content you want to reuse.

These main functions include:
* include() and include_once()
* require() and require_once()

1.include() function will read the specified file and execute the program inside.
For example: include('/home/me/myfile');
The program code in the imported file will be executed, and when executed, these programs will have and call the source file The include() function is located in the same variable scope. You can import static files from the same server, or even import files from other servers by combining the include() and fopen() functions.

2. The function of include_once() is almost the same as include()
The only difference is that the include_once() function will first check whether the file to be imported is already in the program has been imported elsewhere, if so, the file will not be imported again (this function is sometimes very important, for example, the file to be imported declares some functions that you have defined yourself, then if If you import this file repeatedly in the same program, an error message will occur during the second import, because PHP does not allow functions with the same name to be declared a second time).

3.require() function will read the contents of the target file and replace itself with the read contents.
This reading and substitution action occurs when the PHP engine compiles your program code, not when the PHP engine starts executing the compiled program code (the way the PHP 3.0 engine works) It compiles and executes one line at a time, but this has changed with PHP 4.0. PHP 4.0 first compiles the entire program code, and then executes the compiled program code at once. During the compilation process, nothing will be executed. program code). require() is usually used to import static content, while include() is suitable for importing dynamic program code.

4. Like the include_once() function , the require_once() function will first check whether the content of the target file has been imported before. If so, it will not import the same file again. content.
I personally use the require() function to import copyrights, static text, or other program code that does not contain variables, or that needs to rely on other executed programs to execute correctly. For example:

Copy code The code is as follows:


Webpage Title

[A bunch of content]
// Import copyright declaration text
require('/home/ me/mycopyright');
?>



On the other hand, I usually use include( ) function to import some function libraries or similar program code:
Copy the code The code is as follows:

// Import my function library
include('/home/me/myfunctions');
// Use the PHP functions defined in the previously imported function library to perform some functions
? >



The first logical question you might ask next is: "Where do these imported files go?" The short answer is: "Anywhere in the server file system." "However, it should be noted that if the imported files contain some sensitive information in addition to simple program code fragments, such as accounts and passwords used to connect to the database system, then it is recommended that you do not put these files on the Web server. under the file root directory, because then others can easily steal the information.

You can place these included files in any directory of the system. The only condition is that the identity used by PHP itself to execute (www, nobody or other identities) must have sufficient permissions to read these files. That's it. The extension of these files can also be chosen arbitrarily, even if there is no file extension, it doesn't matter.

Make good use of include() and require() to reasonably divide the shared content that often needs to be changed in the website. It will be much easier to update the website content.

Use PHP to maintain the file system
PHP provides many functions related to the file system, allowing us to not only open files, but also display the contents of the directory, move the file location, and other more Versatile. Some friends have even written PHP programs that can manage file content through the browser.

Before we start to introduce the file system-related functions of PHP, we must first clarify one thing: in the Windows operating system, the file path can be represented by slashes (/) or backslashes (), but in other In the operating system, we only use slashes. To maintain uniformity, the file paths in the examples below use slashes.

In the following example program, I will teach you the basic directory content display function. Each step has comments, please read directly.

Copy code The code is as follows:

$dir_name = "/home/me/";
/* The opendir() function will open a directory and return a reference value (handle) that we can use in The directory is referenced in the program*/
$dir = opendir($dir_name);
/* Begin to create a string. This string contains HTML list tags to display the file names in the directory. . */
$file_list = "
    ";
    /* Use a while loop statement to read all the files in the previously opened directory. If the file name read is not "." or "..", the file name is written into the aforementioned string. */
    while ($file_name = readdir($dir)) {
    if (($file_name != ".") && ($file_name != "..")) {
    $file_list . = "
  • $file_name";
    }
    }
    /* Add the end to the HTML list tag*/
    $file_list .= "
";
/* Close the previously opened directory and end this PHP program*/
closedir($dir);
?>





Files in:







After the above steps, you have successfully converted a certain The file names in the directory are displayed on the web page. But you have to remember one thing: to read a directory or file (the method of reading the file content will be introduced later), the identity used by PHP itself to execute must have at least read permission for the directory or file, otherwise the system An Insufficient Permissions error message will be displayed.

In the next example I will teach you how to copy a file:

Copy code The code is as follows:

$original = "/home/me/mydatabasedump";
$copied = "/archive/mydatabasedumo_1010";
/* Call the copy() function to copy the file from the original location to the new location. If copying is not possible, program execution is terminated and an error message is displayed. */
@copy($original, $copied) or die("Unable to copy file.");
?>

The above example program can be used to expand into a file backup system program. When this program is executed, it will copy the database data files to other directories for backup purposes. As long as we modify the system's schedule file content (crontab), we can have this program automatically execute once a day at a fixed time to achieve automatic system backup without the need for manual execution.

If you have Lynx software installed on your system (Lynx is a text-only Web browser), you can add the following record to the system schedule file to allow the system to automatically activate Lynx at a fixed time and call The PHP backup program we wrote before. When Lynx calls (browses) our PHP program, the program is executed and a backup file is generated. The following example teaches you how to execute our backup program at five o'clock every morning, and automatically close the Lynx program after execution:
0 5 * * * [username] lynx -dump http:// localhost/copyfile.php 1>/dev/null 2>&1
If your system is installed with the CGI version of PHP, then you can directly call the PHP executable file without calling our PHP through Lynx Program
What is the difference between include and require in php?
Usually there is no difference.
When the file to be loaded does not exist , include will give a warning warning, and then continue running. And require will give a fatal error, directly ending the script
======================== ==============================
The PHP manual says this:
require() and include() are identical in every way except how they handle failure. include() produces a warning and require() causes a fatal error. In other words, if you want to stop processing the page if a file is missing, don't hesitate to use require(). This is not the case with include() and the script will continue to run.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327299.htmlTechArticlePHP has the characteristics of fast, reliable, cross-platform application, open source code, etc., making PHP the most popular server One of the terminal Script languages. Based on what I have experienced at work, I share with you...
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