The difference between include require include_once require_once in PHP

Source: Internet
Author: User
Ready to do a two-time development on a PHP open source program, and take this opportunity to learn a little bit about PHP system. I have probably learned a little bit of PHP, but because the work has not been used in this language, so slowly also put down. It took so long a detour to understand that project-driven is the best way to learn. Only when you need to use a language, to learn it will have a better effect, but also not easy to forget.

Two times before the development of the original program to understand the entire structure, in the process of looking at the source of the introduction of a large number of introduced statements, remember before the PHP include and require the difference has been very vague, can not use such attitude to study, So stop to see the connections and differences between PHP and include and require.

The first include and require are introduced to the specified file. _once means that it is introduced only once, which is no longer introduced.

For example, 1.php has a simple print echo ' 1
' The results of the following program run:

Phpinclude' 1.php ';require' 1.php ';include_once' 1.php ';require_once' 1.php '; Will be

1

1

Instead of

1

1

1

1

If the statements introduced by _once are placed above the include and require, the result will be

1

1

1

1

The difference between include and require

1, loading failure processing method is different The biggest difference between include and require, except in the way in which files are introduced, is:

Include generates a warning when a file is introduced and the script will continue to execute.

Require causes a fatal error and the script stops executing.

PHP include' hello.php ';Echo' World ';?>

If hello.php does not exist, echo ' world ' can continue.

PHPrequire' hello.php ';Echo' World ';?>

If hello.php does not exist, echo ' Hello ' will not be executed and will cease to require.

2, include () is a conditional include function, and require () is an unconditional include function. if ( FALSE ){ include ' file.php ' ; //file.php will not be introduced } if ( FALSE ){ require ' file.php ' ; //file.php will be introduced . }

3. File Reference method Include has a return value, and require does not

$retVal = include (' Somefile . PHP '); if (! Empty ( $retVal )){ Echo "file contains success"; } Else { Echo "file contains failed"; } The files that are referenced when the include () are executed are read and evaluated every time
Require () executes a file that needs to be referenced only once (the Require () statement is actually replaced with the file content to be referenced when executing)

You can see that if you have code that contains one of these directives and code that might execute multiple times, using require () is a high efficiency,
If you read different files each time you execute the code or have loops that pass through a set of file iterations, use the Include (),

Require usually use the method, this function is usually placed in the front of the PHP program, the PHP program before execution, will be read into the require specified by the introduction of the file, so that it becomes a part of the PHP program page. Commonly used functions, you can also use this method to introduce it into the Web page.

Include typically uses methods, which are 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 of executing the program

Also on the question of whether the include and require are appended with parentheses,

Theoretically: include and require have no difference in execution results, but parentheses are less efficient, so there is no parentheses behind the parentheses.


turn from: http://liuzhichao.com/p/1743.html

Above introduces the include require include_once Requi in PHP Re_once the difference, including the aspects of the content, want to be interested in PHP tutorial friends helpful.

  • 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.