From the perspective of best practices, which of the following is the most widely used and more reliable? (Php beginners, Do not spray) from the perspective of best practices, which one is the most used and more reliable? (Php beginners, Do not spray)
Reply content:
From the perspective of best practices, which of the following is the most widely used and more reliable? (Php beginners, Do not spray)
The php script introduces require, and the front-end script uses include. Because require is more strict, a serious error will be reported if it is not introduced successfully, so that the program will stop, but the include will not. We recommend that you use require for program rigor.
The difference between require and include is that the processing method after the reference fails:
A fatal E_COMPILE_ERROR error occurs when require fails;
If the include operation fails, only the E_WARNING warning is generated;
It can be understood as follows:
@ Mgckid is a good practice. That is:
Code that involves rigorous business logic ("Php script 』
) We recommend that you use require. If the reference fails and the execution continues, it may damage important data;
It is only used for Code related to the Fight page ("Front-End Script 』
) You can use include. Even if the reference fails, subsequent operations will not damage the data. This makes the page missing modules or 4xx errors at most easier to accept than 5xx errors.
Check the specific logic. For example, the SQL statement used to operate the database is suitable for require. If the front-end html is introduced, it is suitable for include.
Require_once ("");
If it is a class library, the require references multiple times and reports an error. It is best to use require_once
In comparison, there is another include_once, which can be used when file accessibility is the best.
Incluce loaded when used
Require load at the beginning
* _ Once loaded does not load
include("file.php") include "file.php";
Require unconditional loading include conditional Loading
Use case by case, to see the importance of introducing files