Home > Backend Development > PHP Tutorial > Deeply understand the difference between include_once and require_once_PHP Tutorial

Deeply understand the difference between include_once and require_once_PHP Tutorial

WBOY
Release: 2016-07-21 15:06:28
Original
773 people have browsed it

①Function and usage

Can reduce code duplication

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

②Understanding

To put it bluntly, it means replacing the include(_once) and require(_once) lines

with the content in the included file ③Note 

 include/require The included files must be added with because when including, the first thing to understand is that the file content is an ordinary string. When encountering tag is used to explain

④Path

You can use absolute paths or relative paths; both forward and backward slashes are acceptable under Windows. Linux only recognizes forward slashes, so it is best to use forward slashes

⑤Difference

 include means inclusion. When the file cannot be found, a warning will be reported. error, and then the program continues 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, if it is already included, it will not include it for 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 result The output is 6, and 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, it is the system configuration. If it is missing, the website will not allow it. To run, 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

. The difference in efficiency is whether to add once or not. On once, although the system considers loading only once for you, the system's judgment will be that the efficiency will 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 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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327607.htmlTechArticle①The function and usage can reduce the duplication of code include(_once) ("path to the file") and require(_once ) ("path to the file") ② To put it bluntly, use the included file...
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