Home >Backend Development >PHP Tutorial >Introduction to include() and require() in files in PHP_PHP Tutorial

Introduction to include() and require() in files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:53956browse

This article introduces in detail the file call test in php, including include(), require(), include_once(), require_once() and other exchange calls. Friends in need can refer to it.


7.3.1 Use of Include, you can include the same file multiple times

The code is as follows Copy code
 代码如下 复制代码

include 'demo1.php';

include 'demo1.php';

include 'demo1.php';

?>

include 'demo1.php';

include 'demo1.php';
代码如下 复制代码

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359

include 'demo1.php';

?>

 代码如下 复制代码

 

include_once 'demo1.php';

include_once 'demo1.php';

include_once 'demo1.php';

?>

The output result is as follows

The code is as follows Copy code
e10adc3949ba59abbe56e057f20f883e

7c4a8d09ca3762af61e59520943dc26494f8941b

3.14159265359
 代码如下 复制代码
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
 

-------------------------------------------------- ----------------------------------

e10adc3949ba59abbe56e057f20f883e
 代码如下 复制代码

require 'demo1.php';

require 'demo1.php';

require 'demo1.php';

?>
 

7c4a8d09ca3762af61e59520943dc26494f8941b

3.14159265359

 代码如下 复制代码

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359


--------------------------------------------------------------------------------

e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
 

-------------------------------------------------- ---------------------------------- e10adc3949ba59abbe56e057f20f883e 7c4a8d09ca3762af61e59520943dc26494f8941b 3.14159265359
7.3.2 The use of include_once is no different from include, but calling it multiple times will only include the same file once
The code is as follows Copy code
<🎜>include_once 'demo1.php';<🎜> <🎜>include_once 'demo1.php';<🎜> <🎜>include_once 'demo1.php';<🎜> <🎜>?>
The results are as follows
The code is as follows Copy code
e10adc3949ba59abbe56e057f20f883e 7c4a8d09ca3762af61e59520943dc26494f8941b 3.14159265359
7.3.3 require() statement includes and runs the specified file.
The code is as follows Copy code
<🎜>require 'demo1.php';<🎜> <🎜>require 'demo1.php';<🎜> <🎜>require 'demo1.php';<🎜> <🎜>?>
The results are as follows
The code is as follows Copy code
e10adc3949ba59abbe56e057f20f883e 7c4a8d09ca3762af61e59520943dc26494f8941b 3.14159265359 -------------------------------------------------- ---------------------------------- e10adc3949ba59abbe56e057f20f883e 7c4a8d09ca3762af61e59520943dc26494f8941b 3.14159265359 -------------------------------------------------- ---------------------------------- e10adc3949ba59abbe56e057f20f883e 7c4a8d09ca3762af61e59520943dc26494f8941b 3.14159265359

7.3.4 The require_once() statement includes and runs the specified file during script execution. But the same file is not included repeatedly.

The code is as follows
 代码如下 复制代码

require_once 'demo1.php';

require_once 'demo1.php';

require_once 'demo1.php';

?>

Copy code

代码如下 复制代码
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
require_once 'demo1.php';

require_once 'demo1.php';

require_once 'demo1.php';

?>

 代码如下 复制代码

include 'demo111.php';

echo('this is demo13.php');

?>
 

The output results are as follows

 代码如下 复制代码

Warning: include(demo111.php) [function.include]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo13.php on line 2

Warning: include() [function.include]: Failed opening 'demo111.php' for inclusion (include_path='.;C:php5pear') in D:AppServwwwBasic7demo13.php on line 2
this is demo13.php

7.3.5 The difference between include and require

If there is other code after Include, when an error occurs when calling include, the following code will continue to execute, but require will not.
 代码如下 复制代码

require 'demo111.php';

echo('this is demo14.php');

?>
 

Include will give a warning when calling a non-existent file, but will continue to execute the subsequent code.

The code is as follows
 代码如下 复制代码

Warning: require(demo111.php) [function.require]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo14.php on line 2

Fatal error: require() [function.require]: Failed opening required 'demo111.php' (include_path='.;C:php5pear') in D:AppServwwwBasic7demo14.php on line 2

Copy code
<🎜>include 'demo111.php';<🎜> <🎜> <🎜> <🎜>echo('this is demo13.php');<🎜> <🎜>?>
The output results are as follows
The code is as follows Copy code
Warning: include(demo111.php) [function.include]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo13.php on line 2 Warning: include() [function.include]: Failed opening 'demo111.php' for inclusion (include_path='.;C:php5pear') in D:AppServwwwBasic7demo13.php on line 2
this is demo13.php Require will give an error and abort code execution when calling a file that does not exist.
The code is as follows Copy code
<🎜>require 'demo111.php';<🎜> <🎜> <🎜> <🎜>echo('this is demo14.php');<🎜> <🎜>?>
The output results are as follows
The code is as follows Copy code
Warning: require(demo111.php) [function.require]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo14.php on line 2 Fatal error: require() [function.require]: Failed opening required 'demo111.php' (include_path='.;C:php5pear') in D:AppServwwwBasic7demo14.php on line 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631295.htmlTechArticleThis article introduces in detail the file calling test in php including include(), require(), include_once(), require_once() and other exchange calls, friends in need can refer to it...
Statement:
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