Home  >  Article  >  Backend Development  >  How to reference external files in php

How to reference external files in php

(*-*)浩
(*-*)浩Original
2019-09-19 11:18:414024browse

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

How to reference external files in php

## Basic syntax (Recommended learning: PHP programming from entry to proficiency)

require: The require function is generally placed at the front of the PHP script. Before PHP is executed, it will read the file imported by require, include and try to execute the imported script file.

require works by improving 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 imported files, when using loops or conditional statements to introduce files in PHP, you need to use include

include: can be placed in PHP Anywhere in the 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.

include has a return value, while require does not.

include will generate a warning (E_WARNING) when it fails to load the file, 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

The above is the detailed content of How to reference external files in php. 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