Home > Backend Development > PHP Tutorial > The difference between php include_once and require_once

The difference between php include_once and require_once

黄舟
Release: 2023-03-11 12:28:02
Original
1517 people have browsed it

① Function and usage

Can reduce code duplication

include(_once) ("path to file") and require( _once) ("path to the file")

②Understanding

To put it bluntly, it means replacing the include(_once), require(_once) line with the content of the included file

③Note

The included files in include/require must add because when including, first understand that the file content is an ordinary string, when encountering tag is used to explain

④Path

You can use an absolute path or a relative path; both forward and backward slashes are acceptable under Windows, only under Linux Recognize forward slashes, so it is best to use forward slashes

⑤Difference

include means inclusion. When the file cannot be found, a warning error will be reported, and then the program will continue to execute.

require means necessary. When the file cannot be found, a fatal error will be reported and the program will stop executing.

After adding once, the system will judge whether it is already included , it will not include the second time

  eg: There is an a.php file whose content is

   The content in the b.php file is $a=5; require_once("a.php"); echo $a; require_once("a.php"); echo $a;

The first output of the result is 6. The second output is still 6, explain. . _once is only included once. If once is not added, the second output will be 7

⑥Choose

For example, if it is missing in the system configuration, the website will not run, so naturally use require. If it is a certain statistical program, if it is missing, it will only count the number of people on the website. It is not necessary. You can use include

. Adding once is the difference in efficiency. Adding once, although the system will help You considered loading only once, but the system judged that the efficiency would be reduced. Therefore, you should adjust the directory structure at the beginning of development and try not to use _once.

⑦Special Usage

Use include/require to return the return value of the included page

In the a.php page: ..... return $value; In the b.php page: $v = include("a.php");


This is used when configuring the website I'll meet you occasionally!

The above is the detailed content of The difference between php include_once and require_once. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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