Home  >  Article  >  Backend Development  >  How to use require in PHP

How to use require in PHP

Guanhui
GuanhuiOriginal
2020-06-19 16:45:485227browse

How to use require in PHP

Usage of require in PHP

The require statement in PHP is to include and run the specified file. The difference from include: include is introduced If the file is not saved, a warning will be generated and the script will continue to execute, while require will cause a fatal error and the script will stop executing.

Recommended video tutorial: "PHP"

Usage example

<?php
require(&#39;somefile.php&#39;);
?>

<?php
require &#39;somefile.php&#39;;
?>

The PHP script file only reads the files it includes when it reads the include() statement. In this way, the process of program execution can be simplified.

  • include Load when used

  • require Load at the beginning

  • _once suffix Indicates that the loaded one is not loaded

PHP The system has a pseudo-compilation process when loading a PHP program, which can speed up the running speed of the program. But the documentation for include is still interpreted. If there is an error in the include file, the main program will continue to execute. If there is an error in the require file, the main program will also stop. Therefore, if an error in the included file has little impact on the system (such as interface file), use include, otherwise use require.

require() and include() statements are language structures, not real functions. They can be like other language structures in php. For example, echo() can use echo("ab") form, or you can use Echo "abc" format outputs the string abc. The require() and include() statements can also add parameters directly without parentheses.

include_once() and require_once() statements also include running the specified file during script execution. This behavior is similar to include() statements and require(), and can be used in the same way. The only difference is that if the code in the file is already included, it will not be included again. These two statements should be used when the same file may be included more than once during script execution to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.

Recommended tutorial: "PHP"

The above is the detailed content of How to use require 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