PHP contains file functions include, include

Source: Internet
Author: User
Tags require

The

 include (), require () statement contains and runs the specified file. The two structures are exactly the same on the containing file, and the only difference is the handling of the error. The require () statement stops when it encounters a containing file that does not exist, or fails, and complains. Include () continues.

For example, the following code:       Code is as follows: include (' hello.php ');  echo ' include Test final! '; /include the error, but will continue to perform, showing: include Test final!  require (' hello.php ');  echo ' Require Test final! '; /require the error, stop the execution of the code.     A sentence summary: 1.include () produces a warning 2.require ()   leads to a fatal error   in other words, if you want to stop processing the page when you lose the file, don't hesitate to use the  require ()  . Include ()   is not the case, the script will continue to run. Also, be sure to set the appropriate include_path. That is, when the program is require to read the file, rather than parsing, if you can not read the file is require, you can not do the next move. Therefore, not to be properly included will cause the program file, with require better. may also be slightly more efficient.     NOTE: require () will include files in any case, and include () can optionally include:     code as follows: <?php   if (FALSE) {  & nbsp  require (' x.php ');  }   if (FALSE) {     include (' s.php ');  } ?> The code above: x.php   must be included, and & nbsp;s.php   must not be included.   Two ways to provide different use flexibility: require use methods such as require ("myrequirefile.php"); This function is usually placed at the top of the PHP program, before the PHP program is executed, it will read into the file introduced by require, making it a part of the PHP Program Web page. Include use methods such as include ("myincludefile.php");. This function is typically placed in the processing section of the Process Control. PThe HP Program Web page reads the include file when it is read. This way, you can simplify the process of executing your program.     I. Use syntax and introduction   1, include ()   syntax: the include (/path/to/filename) include () statement contains a file at the location where it is invoked. An effect that contains a file with the same content as the data that copies the file at the location of the statement. Parentheses can be omitted when using include ().   can execute the include () statement according to the criteria. Using include () in a conditional statement is a strange phenomenon that must be enclosed in a statement block brace or surrounded by other statement-bounding characters.   2, include_once ()   Syntax: include_once (filename)   include_once () statement contains and runs the specified file during script execution. This behavior is similar to the include () statement, except that include_once () first determines whether the file has been included before and, if it is already contained, ignores the inclusion. Include_once () should be used in nested inclusions to ensure that it is included only once to avoid problems such as function redefinition, variable assignment, and so on.   Summary: the include_once () function works the same as include, but it first verifies that the file is already included. If it is already included, the include_once is no longer executed. Otherwise, you must include the file. Except this is exactly the same as include.   3, require ()   syntax: require (filename) require () is, to a large extent, the same as include, including a template file to the location where the require call is sitting. There are two important differences between require and include. First, regardless of the location of the require, the documentation will be included in the script that appears require. For example, even if require is placed in an if statement that evaluates to FALSE, the specified file is still included. The second important difference is that the script will stop running when the require error occurs, and the script will continue to execute if the include is used.   4, require_once ()   Syntax: require_once (filename) require_once () statement contains and runs the specified file during the execution of the script. This behavior and RequirE () statement is similar, the only difference is that require_once () first determines whether the file has been included before, if it is already included, ignore the inclusion. Require_once () should be used in nested inclusions to ensure that it is included only once to avoid problems such as function redefinition, variable assignment, and so on.   Summary: As the site grows larger, there may be duplicates containing certain files. This may not be a problem, but it may not be desirable to have a variable that contains files that are overwritten after they are again included in the original file. There may also be another problem, that is, the conflict of function names in the included file. Using require_once can solve these problems. The Require_once function ensures that the file is included only once. After Require_once is encountered, subsequent attempts to include the same file are ignored.     II, Difference summary   1, include () and require () statements. The difference: The two structures are exactly the same except how to handle failures. Include () generates a warning that the script will continue to run. Require () causes a fatal error and the script stops running.   In other words, if you want to stop processing the page when you encounter a missing file or encounter an error, use require (). Use include () if you want to continue processing the page if you encounter an error. Note that the syntax error in the include file does not cause the program to stop until PHP 4.3.5, but after this version. The difference between   2, include_once (), require_once () and include (), require (), include_once () and require_once (), Should be used in cases where the same file may be included more than once during the execution of a script, to ensure that it is included only once to avoid problems such as function redefinition, variable assignment, and so on. This is the main difference between include_once () and require_once () and include () and require ().     III. issues to be noted   1. Path problems especially when nested inclusions, be sure to note the path to the containing file. For example, a file contains b files, b file contains c files, a,b,c files are not in the same folder, this time is often very easy to make mistakes. Solution: You can use the DirName (__file__) statement, which means to get the absolute path of the current script. such as: require_once (DirName (__file__). /confIg.php ');   2. Efficiency issues include_once (), require_once (), with include (), require (), less efficient, because at least they must first determine whether the file is included. The problem has been greatly improved in the PHP5 version, but there is still a difference in efficiency.

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.