An in depth understanding of the differences between require and require_once versus include and include_once

  • 2020-06-07 04:06:49
  • OfStack

PHP is fast, reliable, cross-platform and open source, making it one of the most popular server-side Script languages. Based on what I have learned in my work, I would like to introduce the use of PHP to you. I hope it will be helpful to you.

Use Include files to maintain your site
Regardless of the size of your site, it's important to recognize the importance of reusing program code, whether you're using PHP or HTML source code. For example, the copyright notice at the end of a website should be changed at least once a year. What if your website has many pages? Changing these pages one by one is definitely a headache. With PHP we can reuse program code in several different ways. Which function ends 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. The include() function reads the specified file in and executes the program.
For example: include ('/home me/myfile ');
The program code in the imported file will be executed, and these programs will have the same variable range (variable scope) at the point of execution that the source file calls the include() function. 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 include_once() function is almost identical to include()
Only one difference between include_once () function will first check to import file is already in other areas of the program to be imported, if any will not repeat import this file (this feature is very important, sometimes say you want to import the archive announced 1 some of your own defined function, so if in repeat this file to import, with a program in the second import will occur when the error message, because PHP are not allowed to repeat declared a function of the same name was 2 times).

3. The require() function reads in the contents of the target file and replaces itself with the contents.
This read and substitution of the movement is in the PHP engine when compile your code, rather than in PHP engine start to perform compiled code (PHP 3.0 engine works is to compile line 1 row 1, but by the PHP 4.0 change, PHP 4.0 is to compile the whole program code is complete, then insert the compiled code, one has been completed in the process of compiling will not perform any 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 checks to see if the contents of the target file have been imported before, and if so, it does not import the same content again.
My personal habit is to use the require() function to import copyright declarations (copyrights), static text or other code that does not itself contain variables, or that relies on other executed programs to execute correctly. Such as:

<HEAD><TITLE> The page title </TITLE></HEAD>
[1 Heap contents ]
//  Import copyright declaration text 

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

//  Import my library 
//  Use the library definitions from the previous import  PHP  Function performs 1 Some functions 
<HEAD><TITLE> The page title </TITLE></HEAD>
[1 Heap contents ]

Then you might ask the first logical question: "Where are these imported files?" The short answer is: "Anywhere in the server file system." Be careful, however, is if the import files in addition to simple program code snippet includes 1 some sensitive information, such as connecting database system is to use the account and password, then I suggest you don't put these files in Web server files under the root directory, because others can easily steal the information.

You can place these included files in any directory of the system, provided that the identity used by PHP itself (www, nobody or other identity) has sufficient permissions to access the files. These files can also have any extension, even without an attached file name.

Making good use of include() and require() to properly split up the Shared content within your site that often needs to change will make updating your site much easier.

Use PHP to maintain file systems
PHP provides a number of file-system-related functions that allow you not only to open files, but also to display the contents of directories, the location of files to be moved, and much more. Some friends have even written PHP programs that manage the contents of a file through a browser.

Before we get into PHP's file system features, we need to clarify one thing: in Windows, file paths can be represented as slashes (/) or backslashes (\), but in other operating systems we only use slashes. In order to maintain uniformity, the file paths in the following examples use slashes.

In the following example program I will teach you the basic function of displaying the contents of the directory. There are annotations for each step. Please read directly.

<? /* $dir_name  The value of this variable is the full path to the directory you want to read  */ 
$dir_name = "/home/me/"; 
/* opendir() The function opens a directory and returns 1 A reference value ( handle ) allows us to refer to this directory in a program  */ 
$dir = opendir($dir_name); 
/*  Start building 1 A string, and this string contains  HTML  Is used to display file names in directories.  */ 
$file_list = "<ul>"; 
/*  use 1 a  while  The loop statement reads all the files in the previously opened directory 1 Times. If the file name read is not" . "Or" .. ", writes the file name into the previously mentioned string.  */ 
while ($file_name = readdir($dir)) { 
if (($file_name != ".") && ($file_name != "..")) { 
$file_list .= "<li>$file_name"; 
/*  On behalf of  HTML  List volume label with end  */ 
$file_list .= "</ul>"; 
/*  Close the previously opened directory and close the section  PHP  The program  */ 
<!-- HTML The source code starts here  -->
<!--  use  PHP  Program to display the directory name we read on the page  -->
<P>Files in: <? echo "$dir_name"; ?></p>
<!--  use  PHP  The program displays the filename read from the directory on the page  -->
<? echo "$file_list"; ?>

After the above steps, you have successfully displayed the file name of a directory on the web page. But remember one thing: to read a directory or file (read the file contents later), PHP itself must use an identity that has at least read the directory or file, or the system will display an error message with insufficient permissions.

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

<? /*  variable $orginal Store the full path of source files, variables $copied Store the full path of a new file that replicates the past  */ 
$original = "/home/me/mydatabasedump";
$copied = "/archive/mydatabasedumo_1010"; 
/*  call  copy()  Function to copy files from the original location 1 To a new place. If replication is not possible, the execution of the program is terminated and an error message is displayed.  */
@copy($original, $copied) or die(" Unable to copy file. "); 

The example above can be used to expand into a file backup system program. When this program executes, it copies the database's data files to another directory for backup purposes. As long as we modify the contents of the system scheduling file (crontab), we can make this program automatically execute once a day at a fixed time to achieve automatic system backup without manual execution.

If you have Lynx installed on your system (Lynx is a text-only Web browser), you can add this note to your system scheduler to have the system automatically activate Lynx at a fixed time and call the PHP backup program we wrote earlier. When Lynx calls (browse) our PHP program, it is executed and produces a backup file. Here's an example of how to run our backup program at 5am every morning and automatically shut down the Lynx program when it's finished:
0 5 * * * [username] lynx - dump http: / / localhost copyfile. php 1 > 2 > / dev null & 1
If you have the CGI version of PHP installed on your system, you can call the PHP execution file directly instead of calling our PHP program through Lynx
What is the difference between include and require in php
Usually it doesn't make a difference.
When the file to be loaded does not exist, include will issue a warning to warning and continue running, while require will issue a warning to fatal error to terminate the script
The php manual says:
require() and include() are exactly the same in every way except how to handle failure. include() generates a warning and require() causes a fatal error. In other words, if you want to stop processing pages if you lose files, don't hesitate to use require(). include() does not, and the script continues to run.

Related articles: