Four ways to introduce files in PHP and the differences

Source: Internet
Author: User
Tags php language


Overview and Basic syntax

There are 4 file load statements:http://www.php.cn/wiki/137.html "target=" _blank ">include,require,Include_ Once,require_once.

    • The Require function is usually placed at the front of the PHP program, and before the PHP program is executed, it is read into the file specified by require and made into a part of the PHP program's Web page.

    • The include function is generally placed in the processing part of the process control. The PHP Program page reads the include file before it is read in. This way, you can simplify the process when the program executes.

    • include has a return value, and require does not .

    • "Require" works in order to make the PHP program more efficient, when it is explained in the same PHP page once, the second occurrence will not be explained, this is its advantage. However, strictly speaking, this is also its only disadvantage, because it does not repeat the introduction of the file, so when the PHP Web page using loops or conditional statements to introduce a file, "require" will not make any changes. When there is a situation like this, you must use the "include" command to introduce the file.

    • When PHP encounters a file that is introduced using the "include" approach, it is interpreted once, and when it encounters a second time, PHP interprets it again. "Include" is more efficient than "require", and when a user-defined function is included in the introduction file, PHP has a problem with repeating the function during interpretation. But "include" is not without merit, because in the PHP Web page, it will be repeated every time the "include" command will be interpreted once, it is very suitable for use in the loop or conditional judgment of the statement.

    • The "include_once ()" function and the "require_once ()" function will first check that the contents of the target file have been imported before and, if so, will not re-import the same content again.

    • They can be loaded into PHP or HTML files.

File load path problem

Prerequisites: The following illustration example, take include as an example, also applies to the other 3 load statements.
There are 3 types of paths that can be used.

Relative path

is relative to the location of the current Web page file to locate a loaded file location, relying mainly on the following 2 special symbols:
. /: Indicates the current location, which is the directory (folder) where the current Web page file resides;
. . /: Indicates the previous position, that is, the directory where the current Web page file is located;
Use these 2 symbols to express position information, such as:

Include "./page1.php";     A page1.php file that represents the folder in which the current Web page file contains ". /page2.php ";

Absolute path

Absolute paths are also divided into local absolute paths and network absolute paths.

Local absolute path

Include "c:/d1/d2/p1.php";

Special Note: We should not write this local absolute path directly in the code! But, in fact, this local absolute path of the writing is very common!
How to do that, the example is as follows:
  

Network Absolute Path

The actual load here is usually also HTML files, because the server will be executed after the PHP file is returned to include "http://www.abc123.com.index.php";

"No path" (not recommended)

The form is not given the path information, but only give the filename, not recommended.
Like what:

Inclue "page1.php"; This is usually the case when the PHP language engine is looking for the file in the current page directory.

For path problems see: Relative paths and absolute paths

Document loading and execution process

First step: Exit PHP script mode from the Include statement (enter HTML code mode)
Step two: Load the code in the file set by the include statement and execute it (as in the current file)
Step three: Exit HTML mode, re-enter PHP script mode, continue after the code

The difference between 4 load statements

The difference between include and require

Include loading the file fails (that is, the file is not found), report a "prompt error", and then continue to execute the subsequent code;
Require the file fails to load, an error occurs and the execution is immediately terminated.
Typically, require is used in programs where subsequent code depends on the loaded file.

The difference between include and include_once

Include loaded files do not determine whether to repeat, as long as there is an include statement, will be loaded once-that is, it may cause a duplicate loading.
Include_once loaded file will have internal judging mechanism whether the "previous code" has been loaded, it is no longer loaded.

The difference between include_once and require_once

Same as the difference between include and require

The difference between require and require_once

Same as the difference between include and include_once

The function of the return statement in the loaded file

Include has a return value, and require does not
1. A load statement, if the load succeeds, there will be a return value of 1, if the load fails, the return is false (usually do not use the return value)

However, if there is a return statement loaded into the file, there is another mechanism and effect at this point:
The function of the 2.return statement at this time is to terminate the loading process-the subsequent code of the return statement (which is loaded into the file) is no longer loaded.

The 3.return statement can also be used to return a data when loading a file, in the form: return XX;

Overview and Basic syntax

There are 4 file load statements:include,require,include_once,require_once.

    • The Require function is usually placed at the front of the PHP program, and before the PHP program is executed, it is read into the file specified by require and made into a part of the PHP program's Web page.

    • The include function is generally placed in the processing part of the process control. The PHP Program page reads the include file before it is read in. This way, you can simplify the process when the program executes.

    • include has a return value, and require does not .

    • "Require" works in order to make the PHP program more efficient, when it is explained in the same PHP page once, the second occurrence will not be explained, this is its advantage. However, strictly speaking, this is also its only disadvantage, because it does not repeat the introduction of the file, so when the PHP Web page using loops or conditional statements to introduce a file, "require" will not make any changes. When there is a situation like this, you must use the "include" command to introduce the file.

    • When PHP encounters a file that is introduced using the "include" approach, it is interpreted once, and when it encounters a second time, PHP interprets it again. "Include" is more efficient than "require", and when a user-defined function is included in the introduction file, PHP has a problem with repeating the function during interpretation. But "include" is not without merit, because in the PHP Web page, it will be repeated every time the "include" command will be interpreted once, it is very suitable for use in the loop or conditional judgment of the statement.

    • The "include_once ()" function and the "require_once ()" function will first check that the contents of the target file have been imported before and, if so, will not re-import the same content again.

    • They can be loaded into PHP or HTML files.

File load path problem

Prerequisites: The following illustration example, take include as an example, also applies to the other 3 load statements.
There are 3 types of paths that can be used.

Relative path

is relative to the location of the current Web page file to locate a loaded file location, relying mainly on the following 2 special symbols:
. /: Indicates the current location, which is the directory (folder) where the current Web page file resides;
. . /: Indicates the previous position, that is, the directory where the current Web page file is located;
Use these 2 symbols to express position information, such as:

Include "./page1.php";     A page1.php file that represents the folder in which the current Web page file contains ". /page2.php ";

Absolute path

Absolute paths are also divided into local absolute paths and network absolute paths.

Local absolute path

Include "c:/d1/d2/p1.php";

Special Note: We should not write this local absolute path directly in the code! But, in fact, this local absolute path of the writing is very common!
How to do that, the example is as follows:
  

Network Absolute Path

The actual load here is usually also HTML files, because the server will be executed after the PHP file is returned to include "http://www.abc123.com.index.php";

"No path" (not recommended)

The form is not given the path information, but only give the filename, not recommended.
Like what:

Inclue "page1.php"; This is usually the case when the PHP language engine is looking for the file in the current page directory.

For path problems see: Relative paths and absolute paths

Document loading and execution process

First step: Exit PHP script mode from the Include statement (enter HTML code mode)
Step two: Load the code in the file set by the include statement and execute it (as in the current file)
Step three: Exit HTML mode, re-enter PHP script mode, continue after the code

The difference between 4 load statements

The difference between include and require

Include loading the file fails (that is, the file is not found), report a "prompt error", and then continue to execute the subsequent code;
Require the file fails to load, an error occurs and the execution is immediately terminated.
Typically, require is used in programs where subsequent code depends on the loaded file.

The difference between include and include_once

Include loaded files do not determine whether to repeat, as long as there is an include statement, will be loaded once-that is, it may cause a duplicate loading.
Include_once loaded file will have internal judging mechanism whether the "previous code" has been loaded, it is no longer loaded.

The difference between include_once and require_once

Same as the difference between include and require

The difference between require and require_once

Same as the difference between include and include_once

The function of the return statement in the loaded file

Include has a return value, and require does not
1. A load statement, if the load succeeds, there will be a return value of 1, if the load fails, the return is false (usually do not use the return value)

However, if there is a return statement loaded into the file, there is another mechanism and effect at this point:
The function of the 2.return statement at this time is to terminate the loading process-the subsequent code of the return statement (which is loaded into the file) is no longer loaded.

The 3.return statement can also be used to return a data when loading a file, in the form: return XX;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.