Include ()
The include () Statement determines at the position where it is called and contains a file. Contains the result that the data of a file is the same as that of the file where the statement is located. The format is:
Include (/path/to/filename)
Like print and echo statements, parentheses can be ignored when include () is used. For example, if you want to include a series of predefined functions and configuration variables, you can put these functions and configuration variables in a separate file (for example, the name is init. PHP), and then include the file at the top of each PHP script, as shown below:
<?PHPInclude"/User/local/lib/PHP/wjgilmore/init. Inc. php";/*The script continues here*/?>
You can also execute the include () statement according to the conditions. For example, if an Include () Statement is placed in an if statement, the file is contained only when the if statement is true. The use of include () in conditional statements has a strange phenomenon.It must be enclosed in statement block braces or enclosed by other statements. Consider the following two endsCodeThe syntax is different.
The first code segment is incorrect because it does not have an appropriate block break.
<?PHPIf(Expression)Include('Filename');ElseInclude('Another _ filename');?>
The second code segment is the correct usage. Here, the include () Statement is enclosed in parentheses:
<?PHPIf(Expression ){Include('Filename');}Else{Include('Another _ filename');}?>
There is a misunderstanding about the include () statement, that is, because the included code will be embedded in the PHP Execution block, PHP escape flag is not required. However, this is not the case; the delimiters must always be used. Therefore, do not expect correct parsing by placing the php Command in a file, as shown below:
Echo"This is an invalid include file ";
In the world, any PHP statement must be surrounded by correct escape tags, as shown below:
<?PHPEcho"This is an invalid include file";?>
All the code in the contained file will integrate the variable scope at the caller's location.
The include () statement can also contain files on the remote server. If the server where the file is located supports PHP, pass the necessary key-value pairs, You can parse the variables in the included file like the GET request, as shown below:
Include "http://www.wjgilmore.com/index.html? Background = Blue ";
Two conditions must be met when the remote file is included.
First, the allow_url_fopen configuration command must be started.
Second, the URL package must be supported.
Include_once () // ensure that the file is only contained once
The role of the include_once () function is the same as that of include (), but it first verifies whether the file is included. The format is as follows:
Include_once (filename)
Include_once () is the same as include ().The statement must be enclosed in braces.
Require () // request file
To a large extent, requirt () is the same as include (). A template file is included in the location where require () is called. The format is as follows:
Require (filename)
There are two important differences between require () and include.
First, regardless of the location of require (), the specified file will be included in the script where require () appears. For example, even if require () is placed in an if statement that is calculated as false, the specified file is still contained.
Only when allow_url_fopen is enabled (this is the default value) Can the URL be used in require.
The second important difference is that when a require () error occurs, the script stops running, and when include () is used, the script continues to run. One possible error is that the require () Statement incorrectly references the target path.
Require_once () // ensure that the file is requested only once
Require_once (filename)
The require_once () function ensures that the file is only contained once. When require_once () is encountered, the attempt to include the same file will be ignored.
Except for the verification process of require_once (), all other aspects are the same as require.