Home > Backend Development > PHP Problem > There are several ways to reference files in php

There are several ways to reference files in php

青灯夜游
Release: 2023-03-09 16:38:01
Original
6682 people have browsed it

There are 4 ways for php to reference files: 1. Use the include() function, which can be placed anywhere in the PHP script; 2. require() function, usually placed at the front of the PHP script; 3. include_once () function; 4. require_once() function.

There are several ways to reference files in php

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

There are four statements for loading files in PHP: include, require, include_once, require_once.

Basic syntax

require: The require function is generally placed at the front of the PHP script. The require specification will be read in before PHP is executed. file that contains and attempts to execute the imported script file. The way require works is to improve the execution efficiency of PHP. After it is interpreted once in the same web page, it will not be interpreted the second time. But similarly, because it will not repeatedly interpret the imported file, you need to use include when using loops or conditional statements to introduce files in PHP.

include: can be placed anywhere in the PHP script, usually in the processing part of the process control. When the PHP script is executed to the file specified by include, it will be included and attempted to execute. This method can simplify the process of program execution. When encountering the same file for the second time, PHP will still re-interpret it again. The execution efficiency of include is much lower than that of require. At the same time, when the user-defined function is included in the imported file, PHP will have the problem of repeated function definition during the interpretation process. .

require_once/include_once: The functions are the same as require/include respectively. The difference is that when they are executed, they will first check whether the target content has been imported before. If it has been imported, Then the same content will not be reintroduced again.

The difference between each other

include and require:

include has a return value, while require has no return value

include is loading the file On failure, a warning (E_WARNING) is generated and the script continues execution after the error occurs. So include is used when you want to continue execution and output results to the user.

//test1.php
<?php
include &#39;./tsest.php&#39;;
echo &#39;this is test1&#39;;
?>

//test2.php
<?php
echo &#39;this is test2\n&#39;;
function test() {
    echo &#39;this is test\n&#39;;
}
?>

//结果:
this is test1
Copy after login

require will generate a fatal error (E_COMPILE_ERROR) when loading fails, and the script will stop executing after the error occurs. Generally used when subsequent code depends on the loaded file.

//test1.php
<?php
require &#39;./tsest.php&#39;;
echo &#39;this is test1&#39;;
?>

//test2.php
<?php
echo &#39;this is test2\n&#39;;
function test() {
    echo &#39;this is test\n&#39;;
}
?>
Copy after login

Result:

include and include_once:

The files loaded by include will not be judged as to whether they are duplicates. As long as there is an include statement, they will be loaded once (even if duplicate loading may occur). When include_once loads a file, there will be an internal judgment mechanism to determine whether the previous code has been loaded. What needs to be noted here is that include_once is judged based on whether a file with the same path has been previously imported, rather than based on the content of the file (that is, the content of the two files to be imported is the same, and using include_once will still introduce two).

//test1.php
<?php
include &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1this is test2


//test1.php
<?php
include &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include_once &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1


//test1.php
<?php
include_once &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1this is test2


//test1.php
<?php
include_once &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include_once &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1
Copy after login

require and require_once: The same difference as include and include_once.

Execution process when loading

1. Exit php script mode from the include (require) statement (enter html code mode)

2. Load the settings set by the include statement The code in the file and try to execute

3. Exit the html mode, re-enter the php script mode, and continue the execution of the subsequent script program

//test1.php
<html>
<body>
主文件开始位置:
<?php
    echo "<br> 主文件中位置 A";
    include "./test2.php";    //要载入的文件
    echo "<br> 主文件中位置 B";
?>
<br> 主文件结束位置
</body>
</html>

//test2.php
<br> 被载入文件位置 1
<?php
echo "<br> 被载入文件位置 2";
?>
<br> 被载入文件位置 3
Copy after login

Result analysis:

Comparison of return values

As mentioned above, include has a return value, but require does not. value

对于include,如果载入成功,有返回值,返回值为1;如果载入失败,则返回false.

对于require,如果载入成功,有返回值,返回值为1;如果载入失败,无返回值。

//test1.php
<?php

$a = include "./test2.php";
var_dump($a);
echo "<br>";

$b = include "./test2.phps";
var_dump($b);
echo "<br>";

$c = require "./test2.php";
var_dump($c);
echo "<br>";

$d = require "./test2.phps";
var_dump($d);

?>
Copy after login

输出:

There are several ways to reference files in php

当文件中有return:

当被载入文件中有return语句时,会有另外的机制,此时return语句的作用是终止载入过程,即被载入文件中return语句的后续代码不再载入。return语句也可以用于被载入文件载入时返回一个数据。

//test1.php
<?php
$a = include "./test2.php";
echo "<br>";
var_dump($a);
?>


//test2.php
//该文件中有return语句
<?php
$b = &#39;test2&#39;;
echo "被载入的文件:A 位置";
return $b;
echo "<br 被载入的文件: B 位置";
?>
Copy after login

结果:

There are several ways to reference files in php

推荐学习:《PHP视频教程

The above is the detailed content of There are several ways to reference files in php. For more information, please follow other related articles on the PHP Chinese website!

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