Home>Article>Backend Development> What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code)

What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code)

Jennifer Aniston
Jennifer Aniston Original
2018-07-23 17:09:37 4124browse

What are the ways to import files in PHP?The PHP import file has four statements:include, require, include_once, require_once. Let’s take a look at specific examples of PHP import files.

Basic syntax

require:require function is generally placed at the front of the PHP script and will be executed before PHP First read in the imported file specified by require, include and try 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 Return value, while require has no return value

When include fails to load a file, it will generate a warning (E_WARNING), and the script will continue to execute after the error occurs. So include is used when you want to continue execution and output results to the user.

//test1.php  //test2.php  //结果: this is test1

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  //test2.php 

Result:

What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code)

include and include_once:

The files loaded by include will not be judged as duplicates , as long as there is an include statement, it will be loaded once (even if repeated 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  //test2.php  //结果: this is test2this is test1this is test2 //test1.php  //test2.php  //结果: this is test2this is test1 //test1.php  //test2.php  //结果: this is test2this is test1this is test2 //test1.php  //test2.php  //结果: this is test2this is test1

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 code in the file set by the include statement and try to execute it

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

//test1.php   主文件开始位置:  主文件中位置 A"; include "./test2.php"; //要载入的文件 echo "
主文件中位置 B"; ?>
主文件结束位置 //test2.php
被载入文件位置 1 被载入文件位置 2"; ?>
被载入文件位置 3

Result:What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code)

Analysis:

What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code)

##Path problem during loading

Relative path:

Locate the location of a loaded file relative to the location of the current web page file.

./ 表示表示当前位置,即当前网页文件所在的目录 . . / 表示上一级位置,即当前网页文件所在目录的上一级目录 //例如: include "./test2.php"; require "../../test3.html";

Absolute path:

is divided into local absolute path and network absolute path

Local absolute path:

Search recursively from the local root directory layer by layer until you find the file to be imported in the corresponding directory.

include "C:/PHP/test/test2.php";

We all know that absolute paths are not conducive to the portability and maintainability of the project, so it is generally rare to write absolute paths directly in the code, but what should we do if we need to use absolute paths? ? There are magic constants __DIR__ and global array $_SERVER in PHP. The usage is as follows:

Absolute network path:

Link to the file through the URL, and the server will The file pointed to by the URL will be returned after execution

include "http://www.lishnli/index.php"

No path:

Only the file name is given but no path information is given. At this time, PHP will be in the current web page directory. Search for the file. If a file with the same name is found, execute it and import it.

需要注意:无论采用哪种路径,必须要加上文件后缀名,这四种文件载入方式不能识别无后缀的文件。

//test1.php include "./test2.php"; //结果:this is test2 //test1.php include "./test2"; //结果:

返回值的比较

上文说道include有返回值,而require无返回值

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

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

//test1.php "; $b = include "./test2.phps"; var_dump($b); echo "
"; $c = require "./test2.php"; var_dump($c); echo "
"; $d = require "./test2.phps"; var_dump($d); ?>

输出:

What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code)

当文件中有return:

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

//test1.php "; var_dump($a); ?> //test2.php //该文件中有return语句 

结果:

相关推荐:

php引入css文件出错,但是网页已经有样式了

php 字符串写入文件或追加入文件(file_put_contents)

The above is the detailed content of What are the ways to import files in PHP? Introduction to four methods of introducing files in PHP (code). For more information, please follow other related articles on the PHP Chinese website!

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