Home  >  Article  >  Backend Development  >  What are the differences between php require_once and include?

What are the differences between php require_once and include?

coldplay.xixi
coldplay.xixiOriginal
2020-07-01 15:26:342589browse

php The differences between require_once and include are: 1. [require_once] applies to a file and can only be applied once. If the file does not exist, program execution will be interrupted; 2. [include()] function will The file is read and the program inside is executed.

What are the differences between php require_once and include?

php The differences between require_once and include are:

1, include() function Will read the specified file and execute the program inside.

For example:

include('/home/me/myfile');

The program code in the imported file will be executed, and when executed, these programs will have the same location as the include() function called in the source file. Variable scope. You can import static files from the same server, or even import files from other servers by combining the include() and fopen() functions.

2, require_once()Its function is the same as require, both refer to or include an external php file

Principle: When containing require_once When the php file is executed, the external files specified by require_once will be included, form a new php file with other codes, and start execution

Note:

  • When there is an error in the external included file, an error will occur and the run will be terminated.

  • require_once If the specified file has already been included, it will not be included again. It can avoid problems such as function redefinition and variable reassignment.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What are the differences between php require_once and include?. 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