PHP include/require/include_once/require_once usage experience, requireonceinclude
I have been taking the time to learn PHP recently. It is said that PHP is the best language in the world, so various forums have exploded! Haha, we are not here to discuss which language is the best language. We are learning the features and differences of the four methods introduced by files that are frequently used in PHP!
I believe that when using PHP, we often see include a. php, include_once a. php, require a. php, or require_once a. php, which is similar to this?
Let's take a look at the functions and features of these statements!
Include
Usage:
include "test.php";
It is generally used in the processing part of process control to introduce the file content. When the PHP program encounters an include statement, it will be read in. This method can simplify the process during program execution and facilitate code reuse!
When a non-stored file is introduced into the include statement, a warning is generated and the script continues to be executed. The file to be referenced during execution must be read and evaluated each time, and a return value exists. For example:
If (FALSE) {include 'test. php'; // test. php will not be introduced.} <? Php include 'test. php'; // The condition is that test. php does not have echo 'technical blog of '; // still execute the following code?> $ Ret = include "QueryPhone. php"; if (! Empty ($ ret) {echo "file introduced successfully";} else {echo "file introduced failed ";}
Include_once
Usage:
include_once "test.php";
After _ once is added, the imported files will not be introduced. When include_once introduces the file, if an error occurs, a prompt is displayed and the following code is continued!
Its usage is similar to include. The difference is that include_once only introduces one article!
Require
Usage:
require "test.php";
Generally, the file content is introduced at the beginning of the PHP file. PHP will introduce the require file content to a part of the current file before executing the subsequent code.
When a file fails to be introduced, require will prompt that the script will be interrupted. For example:
// The file does not exist. require "QueryPhone. php"; echo "not printed ";
Require_once
Usage:
require_once "test.php";
It is generally placed at the beginning of the PHP file to introduce the file content. PHP will first introduce the file content to this file. If the Import fails, it will not continue to be executed. If the import is successful, the following code can be executed normally.
Its usage is similar to that of require. The difference is that require_once will be introduced only once. If it has been introduced before, it will not be introduced again!
Example
Suppose there is a. php file with only one echo file name is;
<?php include 'a.php';require 'a.php'; include_once 'a.php';require_once 'a.php';
So how many rows will the above four introductions print? In fact, only the execution results of the previous two statements are printed, so there are only two:
file name is afile name is a
Next, let's exchange the first two rows and the last two rows:
<?php include_once 'a.php';require_once 'a.php'; include 'a.php';require 'a.php';
How many lines will be printed in the above four sentences? There are four rows, because the first two rows have not been introduced before, so they will be introduced once. Although the include/require has been introduced before, it will be introduced again. Therefore, the result is printed:
file name is afile name is afile name is afile name is a
Notes
Theoretically, there is no difference in the execution result by adding no parentheses after include and require, but the efficiency of adding parentheses is relatively low, therefore, we can leave no brackets behind!