What are the differences between several include file functions in PHP?

青灯夜游
Release: 2023-03-11 15:52:02
Original
2727 people have browsed it

Difference: 1. If include encounters an error, it will give a prompt and continue execution; while require will terminate program execution. 2. If the included file in require_once and include_once has already been included, it will not be included again, but include and require will.

What are the differences between several include file functions in PHP?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

File inclusion refers to the inclusion of another source file All contents are included in the current source file for use, which is also commonly referred to as introducing external files. Referencing external files can reduce code reusability and is an important skill in PHP programming.

PHP provides 4 very simple but useful include statements, namely include statement, require statement, include_once statement and require_once statement. There are certain differences in the use of these four statements. Each is explained in detail below.

1. include statement

When using the include statement to include an external file, the external file will only be included when the code is executed to the include statement. , when an error occurs in the included external file, the system will give a warning, and the entire PHP program will continue to execute.

The syntax format of the include statement is as follows:

include('filename') 或者 include 'filename'
Copy after login

where filename is the file path to be included (both relative and absolute paths are acceptable), filename is a string, so single quotes are required' ' or wrapped in double quotes " ". At the same time, the brackets after include can also be omitted. When the brackets are omitted, include needs to be separated from the following filename by a space.

For the convenience of demonstration, here we prepare a demo.php file and simply define a $str variable in it, as shown below:

<?php
    $str = &#39;https://www.php.cn/&#39;;
?>
Copy after login

[Example] Use the include statement to include the demo .php file, the code is as follows:

<?php
    include &#39;./demo.php&#39;;
    echo $str;
?>
Copy after login

The running result is as follows:

https://www.php.cn/
Copy after login
Copy after login

2. require statement

require The usage of the statement is similar to the include statement, both of which implement references to external files. Before the PHP file is executed, the PHP parser will replace the require statement with the entire content of the referenced file, then form a new PHP file with other statements except the require statement, and finally execute the program code according to the new PHP file.

Note: Because the require statement is equivalent to completely copying the contents of another source file into this file, it is generally placed at the beginning of the source file to reference the public function files and files that need to be used. Public class files, etc.

The require statement is almost exactly the same as the include statement. The difference is that when the included file does not exist or there is an error, the require statement will issue a Fatal error and terminate program execution, while the include statement will issue a Warining warning. But the program will continue to execute downwards.

The syntax format of the require statement is:

require(filename) 或者 require &#39;filename&#39;
Copy after login

The parameter filename is the path of the file to be included, and its characteristics are the same as the parameters in the include statement.

[Example] Use the require statement to include the demo.php file defined above. The code is as follows:

<?php
    require &#39;./demo.php&#39;;
    echo $str;
?>
Copy after login

The running results are as follows:

https://www.php.cn/
Copy after login
Copy after login

3. include_once statement

The include_once statement is similar to the include statement. The only difference is that if the included file has already been included, it will not be included again. include_once can ensure that the same file is only included once during script execution to avoid problems such as function redefinition and variable reassignment.

Let’s adjust the demo.php file defined above as follows:

<?php
    echo &#39;PHP中文网<br>&#39;;
    echo &#39;https://www.php.cn/&#39;;
?>
Copy after login

[Example] Use the include_once statement to include the demo.php file, the code is as follows:

<?php
    include_once &#39;./demo.php&#39;;
    include_once &#39;./demo.php&#39;;
    include_once &#39;./demo.php&#39;;
?>
Copy after login

The running results are as follows:

PHP中文网
https://www.php.cn/
Copy after login
Copy after login

4. require_once statement

require_once statement is an extension of require statement, and its function is the same as require The statements are basically similar. The difference is that when applying the require_once statement, it will first check whether the file to be included has been included elsewhere in the program. If so, the file will not be included again.

[Example] Use the require_once statement to include the demo.php file. The code is as follows:

<?php
    require_once &#39;./demo.php&#39;;
    require_once &#39;./demo.php&#39;;
    require_once &#39;./demo.php&#39;;
?>
Copy after login

The running results are as follows:

PHP中文网
https://www.php.cn/
Copy after login
Copy after login

Recommended learning: "PHP Video tutorial

The above is the detailed content of What are the differences between several include file functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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