php file contains functions
In actual development, it is often necessary to put the common code in the program into a file, and the files using these codes only need to include this file. This method helps improve the reusability of code and brings great convenience to code writing and maintenance. In PHP, there are four methods: require, require_once, include, and include-once to include a file.
Let’s compare their differences:
Function | Contains failure | Characteristics |
---|---|---|
Inlcude | Return a warning | The file continues to execute downwards. Usually used for dynamic inclusion |
Require | A fatal error | The code will not continue to execute. Usually contains extremely important files, the entire code should not be executed |
Include_once | returns a warning | In addition to the original include function, it also Will do once detection. If the file has been included before, it no longer contains |
Require_once | A fatal error | In addition to the original function In addition, a once detection will be done to prevent the file from being included repeatedly |
Note:
1, use _once with once less, because it will consume more resources to do detection work.
2, Extra Advanced
Include files only need to be compiled once, because each time include is included, the corresponding code will be executed again. How to reduce the process of re-parsing when include is executed again.
Let’s do a few experiments:
1, include contains the function of the function.
Create a functions.php file and write two functions in it:
In the same directory as functions.php, I create a user.php file and add the functions.php file included. In this way, my functions can be placed specifically in functions.php. When these functions are needed, I will include them from there:
I know the function of include through the above example. Next, we compare include and require:
In the code, we first use include to include the non-existent test.php file,
and then use require to include the non-existent test.php file:
Through the comparison of the above example, we found:
If the test.php file does not exist include, a warning will be issued and the demo() and test() functions will continue to be executed.
The require will directly report an error, and the demo() and test() functions cannot continue to execute.
We know from the table: The difference between include and include_once is to detect whether it is included repeatedly. If include_once is included repeatedly, the corresponding file will no longer be included, while include does not care about this. Whether the file has been imported or not, import it again.
Let’s try the same user.php just now. We include functions.php twice, using include and include_once respectively:
Change to include_once and try again:
After you execute it separately, you will find that -include reported the following error when introducing functions.php twice this time:
The prompt in the above picture is that the function demo() cannot be redefined.
We mentioned in the function definition chapter that a function cannot be defined twice, otherwise an error will be reported. Because we included functions.php twice, it was executed twice, so this error was reported.
The reason why include_once does not report an error is because it detects that functions.php has been included before and no longer includes it.
As for the functions of require and require_once, can you use your smartest little brain to deduce them? require_once has two characteristics:
1. The included file must exist, otherwise execution will stop
2. Duplicate inclusion checks will be done