In-depth understanding of the differences between require, require_once, include, and include_once _ PHP Tutorial

Source: Internet
Author: User
Understand the differences between require, require_once, include, and include_once. PHP is fast, reliable, cross-platform application, and open source code. This makes PHP one of the most popular server-side Script languages. Based on my experience at work, PHP has the features of fast, reliable, cross-platform application, and open source code to PHP, making PHP one of the most popular server-side Script languages. Based on what I learned at work, I would like to introduce my experiences in using PHP and hope to help you.

Use PHP Include files to maintain your website
Whether the size of the website you develop is large or small, you should realize the importance of reusing the program code, whether you reuse the PHP program or the original HTML code. For example, the copyright notice at the end of a website must be modified at least once a year. what if your website has many pages? It is a headache to modify these pages one by one. Using PHP, we can reuse program code in several different ways. Which function terminals you want to use depends on what you want to reuse.

These main functions include:
* Include () and include_once ()
* Require () and require_once ()

1. the include () function reads the specified file and runs the program.
Example: include ('/home/me/myfile ');
The program code in the imported file will be executed, and these programs will have the same variable scope as the variable scope called to the include () function in the source file during execution ). You can import static files on the same server, or even import files on other servers by combining the include () and fopen () functions.

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

3. The require () function will read the content of the target file and replace itself with the read content.
This read-and substitute action occurs when the PHP engine compiles your program code, rather than when the PHP engine starts to execute compiled program code (the PHP 3.0 engine works by compiling a line to execute a line, but it changes to PHP 4.0, PHP 4.0 is to compile the entire program code first, and then execute the compiled program code once. during the compilation process, no program code will be executed ). Require () is usually used to import static content, while include () is suitable for importing dynamic program code.

4. like the include_once () functionThe require_once () function first checks whether the content of the target file has been imported before. If yes, the same content will not be imported again.
I personally use the require () function to import copyrights, static text, or other non-variables, or you may need to rely on other programs that have been executed to correctly execute the program code. For example:

The code is as follows:



Webpage title

[A bunch of content]
// Import the copyright notice text
Require ('/home/me/mycopyright ');
?>



On the other hand, I usually use the include () function at the beginning of the program to import some function libraries or similar program code:

The code is as follows:


// Import my function library
Include ('/home/me/myfunctions ');
// Execute some functions using the PHP functions defined in the previously imported function library
?>

Webpage title

[A bunch of content]



Next, you may ask the first logical question: "where should these imported files be stored ?」 The short answer is: "Everything in the server file system can be done .」 However, it should be noted that, in addition to simple program code snippets, the imported files also contain some sensitive information, such as the account and password used to connect the database system, we recommend that you do not place these files under the root directory of the files on the Web server, because they can be easily stolen by others.

You can put these contained files in any Directory of the system. The only condition is the identity (www, nobody, or other identity) that PHP uses for execution) you must have sufficient permissions to read these files. The extensions of these archives can also be obtained without any attached names.

Make good use of include () and require () to reasonably split the shared content that often needs to be changed on the website, and 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, so that we can not only open the file, but also display the directory content, the location of the file to be moved, and other functions. Some friends even wrote PHP programs that can manage file content through browsers.

Before introducing the relevant functions of the PHP file system, we need to clear one thing: in the Windows operating system, the file path can use a slash (/) or a backslash (\) but in other operating systems, we only use diagonal lines. To maintain uniformity, the archive paths in the following example use diagonal lines.

The example program below will teach you the basic directory content display function. Each step has an annotation. please read it directly.

The code is as follows:


$ Dir_name = "/home/me /";
/* The opendir () function will open a directory and return a reference value (handle) so that we can refer to this directory in the program */
$ Dir = opendir ($ dir_name );
/* Start to create a string that contains the HTML list volume label to display the file name in the directory. */
$ File_list ="

    ";
    /* Use a while loop to read all the files in the previously opened directory. If the file name is not ".." or "..", write the file name to the string mentioned above. */
    While ($ file_name = readdir ($ dir )){
    If ($ file_name! = ".") & ($ File_name! = "..")){
    $ File_list. ="
  • $ File_name ";
    }
    }
    /* Add the end of the HTML list Volume */
    $ File_list. ="
";
/* Close the previously enabled directory and end the PHP program */
Closedir ($ dir );
?>





Files in:







After the preceding steps, you have successfully displayed the file name in a directory on the webpage. But remember: you need to read a directory or file (the method of reading the file content will be introduced later ), the identity used for PHP execution must have at least the permission to read the directory or file. Otherwise, the system will display an error message with insufficient permissions.

In the next example, I will teach you how to copy an archive:

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 the replication fails, the program is terminated and an error message is displayed. */
@ Copy ($ original, $ copied) or die ("the file cannot be copied. ");
?>


The above example program can be used to expand into a file backup system program. When this program is executed, it copies the database data files to other directories for backup. As long as you modify the system's schedule file content (crontab), we can make this program automatically run once every day to achieve automatic system backup without manual execution.

If Lynx software is installed on your system (Lynx is a text-only Web browser, you can add the following record to the system schedule file to enable 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 will be executed and a backup file will be generated. The following example shows 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 cgi php, you can directly call the PHP execution file without calling our PHP program through Lynx.
What is the difference between include and require in php?
Normally, there is no difference.
When the file to be loaded does not exist, include will give a warning, and then continue to run. while require will give a fatal error, directly end the script
========================================================== ==================
The php Manual says this:
Require () and include () are identical in all aspects except how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when the file is lost, do not hesitate to use require. This is not the case with include (). The script will continue to run.

Bytes. As I learned at work, I want to be large...

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.