What is the difference between php using include and require to introduce files?

怪我咯
Release: 2023-03-12 20:30:01
Original
1144 people have browsed it

The performance of the require() statement is similar to include(), both including and running the specified file. The difference is that for the include() statement, the file is read and evaluated each time it is executed; while for require(), the file is only processed once (in fact, the file content replaces the require() statement ).

Recently, a friend asked some questions about require_once. As for the problems encountered in the program, they are boring and difficult. As long as the bug exists, it is worth exploring and solving it. . In response to this problem, I summarize the detailed usage of include and require. (Integrated by combining the information and my own understanding) I hope it can give some help to friends in need.

Introduction file:

First you need a php file:

<?php
class shao//类名必须和文件名相同!!!
{
  public $xxx="666";
}
$shili = new shao();
echo $shili->xxx;
?>
Copy after login

Quote:

<?php
//引入文件
include "shao.php";//引用文件;只能引用一遍
//引用多个则费劲
//include_once("shao.php");//一次又一次
//也可以require ("shao.php");
?>
Copy after login

The difference between include and require:

require:

When require() is executed, it will only be read once files, so they are often placed at the beginning of the program. After the file is imported, PHP will recompile the web page file so that the imported file becomes part of the original web page.

require(): Unconditionally included. If the file does not exist, a fatal error will be reported. The script will stop executing.
require When an error is encountered, an error will be reported directly and the program will stop running.

include:

When executing include(), the file will be read every time, so it is often used in process control sections, such as Conditional judgment or in a loop.
include(): Conditional inclusion. If the file does not exist, a warning will be given, but the script will continue to execute.
When include encounters an error (the referenced file does not exist), PHP will just report an error, but the program will Continue to run

The following is a supplement:

1. include has a return value, but require does not.
2. include() includes and runs the specified file. When the processing fails, include() generates a warning. The imported program code will be executed, and these programs will have and call include( in the source file when executed. ) statement is located in the same variable scope . You can import static pages from the same server.
3. The function of include_once() is almost the same as include(). The only difference is that include_once() will first check whether the file to be imported has been imported elsewhere in the program. If so, (This function is sometimes very important. For example, if the file you want to import declares some functions that you have defined yourself, then if you import this file repeatedly in the same program, the file will not be imported again in the second import. The error message will occur because PHP does not allow functions with the same name to be declared a second time).
4. require() will read the contents of the target file and replace itself with the read contents. If the processing fails, require() will cause a fatal error. This reading and substitution action occurs when the PHP engine compiles your program code, not when the PHP engine starts executing the compiled program code (the way the PHP 3.0 engine works is to compile and execute one line at a time. But things have changed since PHP 4.0. PHP 4.0 first compiles the entire program code, and then executes the compiled program code at once. No program code will be executed during the compilation process). require() is usually used to import static content, while include() is suitable for importing dynamic program code.
5. Like include_once(), require_once() will first check whether the content of the target file has been imported before. If so, the same content will not be imported again.
6. require is an unconditional inclusion, that is, if require is added to a process, require will be executed first regardless of whether the condition is true or not.
7. require is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.
8. Include is generally placed in the processing part of the process control. The PHP program webpage only reads the included file when it reads it. This method can simplify the process of program execution.

It is recommended to use require_once() and include_once() to detect whether files are included repeatedly.

Auto loading:

##Referenced php file:

<?php
class shao//类名必须和文件名相同!!!
{
  public $xxx="666";
}

?>
Copy after login

Auto loading:

<?php
function autoload($asd)
{
  require $asd.".php";//可填地址查找
}
$shili = new shao();
echo $shili->xxx;
?>
Copy after login


The above is the detailed content of What is the difference between php using include and require to introduce files?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!