Home >Backend Development >PHP Tutorial >Regarding the differences between require, require_once, include and include_once, requireonceinclude_PHP tutorial
1. Definition
Require, require_once, include, and include_once are all PHP keywords, which means they are actually PHP statements, not functions. Similar to print and echo, they are also PHP output statements, not functions. But more often than not, everyone calls it a function.
2. Usage
Require 'test.php', require_once 'test.php', or require('test.php'), require_once('test.php') are all acceptable, and the same is true for include. They are used when one file needs to be introduced into another file. Although they can have parentheses, they are not functions and I prefer to use quotes.
3. The difference between require and require_once
When require() is referenced in the current file, there may be multiple references or duplications of classes or methods. And require_once() receives an address as a parameter. When referencing in the current file, it will first check whether the class or method in the referenced file has been referenced or appears to ensure that the class or method has not been referenced.
4. The difference between include and include_once
Include has the same effect as require, include_once and require_once.
5. The difference between include (include_once) and require (require_once)
When include and include_once reference a file, if an error occurs, a warning will be issued, the referenced file will stop running, and then the current file will continue to run. The overall code operation will not stop.
When require and require_once reference a file, if an error occurs, a warning will be issued immediately, and then the entire code will stop running. Therefore it is safer to use require and require_once correctly.
Six, advantages and disadvantages
require_once takes longer to run, so the efficiency is relatively lower. If time is a priority, require is a better choice, but more often than not, time and efficiency are a balancing act.
Seven, uses
Require and require_once are generally used to include class files. include and include_once are generally used to include template files.
(If there are any inaccuracies, please point them out)