Home  >  Article  >  Backend Development  >  Several methods included in php files

Several methods included in php files

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-26 14:34:442577browse

Several methods included in php files

Four kinds of statements

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

Basic syntax

require: The require function is generally placed at the front of the PHP script. Before PHP is executed, it will first read in the file specified by require, include and try 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: It 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: have the same effect 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, it will not be repeated again. Introduce its same contents.

Related recommendations: "php tutorial"

The difference between each other

include and require:

include There is a return value, but require does not.

include will generate a warning (E_WARNING) when loading a file fails, 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 
<?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

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;; 
} 
?>

Result:

Several methods included in php files

include and include_once:

include The loaded file will not be judged as to whether it is a duplicate, as long as there is an include statement, 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 
<?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

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

The above is the detailed content of Several methods included in php files. 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