Include and require in PHP

Source: Internet
Author: User
Tags flock parse error
PHP include and require reprinted from: http://blog.sina.com.cn/s/blog_5d2673da0100cp6o.html? Include and require in different PHP versions of require (), include (), require_once () and include_once ()

Reprinted from: http://blog.sina.com.cn/s/blog_5d2673da0100cp6o.html

?

Similarities and differences between require (), include (), require_once () and include_once ()

Require () and include () have many similarities and differences. Understanding their differences is very important, otherwise it is easy to make mistakes.

I will describe these two statements together, so readers can learn more.
1. require () statement
???? The require () statement is used to replace the specified file with the statement itself, just like the include () statement in c. If the URL fopen wrappers in the php configuration file php. ini? Yes (opened by default), you can use the URL to specify the location of the file to call the remote file.
???? Pay special attention to the use of require () and include () statements. That is, in the contained file, the processor interprets the content in the html mode, and restores the content to the php mode after processing the included content. Therefore, if you need to use the php syntax in the contained file, you need to use the correct php start and end mark to include these statements.
???? Knowledge of require () and include () is a language feature in php, rather than a function. They are different from functions.
For example, the file contained in require () cannot contain a control structure, and the return statement cannot be used. Using the return statement in files contained in require () produces processing errors.
???? Unlike the include () statement, the require () statement unconditionally reads the content of the files it contains, regardless of whether these statements are executed. Therefore, if you want to include different files according to different conditions, you must use the include () statement. Of course, if the statement where require () is located is not executed, the statements in the file contained in require () will not be executed.
???? Require () cannot contain different files in the loop body based on different conditions. The require () statement will only replace itself with the content in the file it contains when it is executed for the first time. when it is executed again, it can only execute the statements contained for the first time. However, the include () statement can contain different files in the loop body.
???? The variables in the require () statement inherit the scope of the variables where The require () statement is located. All variables that can be accessed at the location of the require () statement can be accessed in the files contained in the require () statement. If the require () statement is inside a function, all the statements in the contained file are defined inside the function.
???? The require () statement reads the file referenced by require before the PHP program is executed. therefore, require is usually placed at the beginning of the program. Therefore, pay special attention to the fact that the require statement is strong. no matter whether the program really needs to reference the file or not, as long as you use the require statement, it will include them! Even if you use this function in a condition control statement to include, the referenced file will be contained even if the condition is not true! The formation of botnets does not have any visible effect during the running process, but it is obvious that it will increase the burden, so pay special attention to this! If an error occurs when you use the require statement, the program outputs the error message and stops running !!

???? If the require () statement declares the URL of the file to contain the remote file, and the remote server interprets the file according to the php code, the content contained in the local php file is the result after being processed on the remote server. For example:
???

Require ("http: // some_server/file.txt? Varfirst = 1 & varsecond = 2 ");


Require ("file. php? Varfirst = 1 & varsecond = 2 ");


Require ("http: // some_server/file. php? Varfirst = 1 & varsecond = 2 ");

$ Varfirst = 1;
$ Varsecond = 2;
Require ("file.txt ");
Require ("file. php ");
???? Originally in php3.0, the return statement can be used for the files contained in require (), but the condition is that the return statement cannot appear inside, it must appear in the global scope of the included files. This feature of require () has been canceled in php4.0, but can still be implemented using include.

2. include () statement
???? The include () and require () statements have many similarities. Unless explicitly stated in the preceding require () statement, the function of the require () statement is applicable to include () statements. The following describes the functions and features of the include () statement not available in the require () statement.
???? The include statement reads the file to be included only when it is executed. The include statement is easy to handle errors. If an include error occurs, the program skips the include statement. Although the error message is displayed, the program continues to execute!
??? The php processor will re-process the include () statement every time it encounters it. Therefore, you can use include () in conditional control statements and loop statements based on different situations () to contain different files.
??? For example:
$ Files = array ('First. php', 'second. php', 'third. php ');
??????? For ($ I = 0; $ I ??????? {
??????? Include $ files [$ I];
}
?>
??? You can use the return statement to return a value in the files contained in the include () statement in php3.0 and php4.0, and stop executing the content under the included file. However, php3.0 and php4.0 are different in handling such situations. In php3.0, the return statement cannot be included in {} unless it is in a function, because it indicates the return value of the function rather than the return value of the file. In php4.0, users can even return a number in the file, just like the return value of the function. Such a statement

Errors are usually reported in php3.0. The following is an example:
??? Assume that the contained file is test. inc and the main file main. php is located in a directory. The content of test. inc is as follows:
Test. inc
Echo "Before the return
\ N ";
If (1)
{
??????? Return 27;
}
Echo "After the return
\ N ";
?>

Assume that the main. php file contains the following statement:
$ Retval = include ('test. Inc ');
Echo "File returned: '$ retval'
\ N ";
?>
???? The php3.0 interpreter reports an error in the second line, but cannot get the return value of the include () statement. However, the following result is displayed in php4.0:
Before the return
File returned: '27'
Next let's assume that main. php is changed:
Include ('test. Inc ');
Echo "Back in main.html
\ N ";
?>
The output result in php4.0 is:
Before the return
Back in main.html

The output result in php5.0 is also:
Before the return
Back in main.html

The output result in php3.0 is:
Before the return
27 Back in main.html

Parse error: parse error in/apache/htdocs/phptest/main.html on line 5

The above error occurs because the return statement is inside {} and not inside a function. If you remove {} to the outermost layer of test. inc, the output result is:
Before the return
27 Back in main.html
The reason why 27 appears is that include () is not supported in php3.0.

3. require_once () and include_once () statements
The require_once () and include_once () statements correspond to the require () and include () statements respectively. The require_once () and include_once () statements are mainly used to avoid repeated function or variable definition errors when you need to include multiple files. For example, if you create two files util. inc and fool. inc, the program code is:
Util. inc:
??? Define (PHPVERSION, floor (phpversion ()));
??? Echo "GLOBALS ARE NICE
\ N ";
??? Function goodTea ()
??? {
?????????? Return "Olong tea tasts good! ";
???}
?>
And fool. inc:
??? Require ("util. inc ");
??? Function showVar ($ var)
??? {
?????????? If (PHPVERSION = 4)
?????????? {
???????????? Print_r ($ var );
??????????}
?????????? Else
?????????? {
???????????? Var_dump ($ var );
??????????}
???}
?>
Then include the two files in error_require.php:
Require ("fool. inc ");
Require ("util. inc"); // This sentence produces an error
$ Foo = array ("1", array ("complex", "quaternion "));
??????? Echo "this is requiring util. inc again which is also
\ N ";
Echo "required in fool. inc \ n ";
Echo "Running goodTea:". goodTea ()."
\ N ";
Echo "Printing foo:
\ N ";
ShowVar ($ foo );
?>
???? When running error_require.php, the output result is as follows:
???? GLOBALS ARE NICE
???? GLOBALS ARE NICE

Fatal error: Cannot redeclare goodTea () in util. inc on line 4
??
If you use the require_once () statement instead of the require () statement, the above error will not occur. Change the require () statement in error_require.php and fool. inc to the require_once () statement and rename it error_require_once.php. The result is as follows:
GLOBALS ARE NICE
This is requiring util. inc again which is also
Required in fool. inc Running goodTea: Olong tea tastes good!
Printing foo:
Array ([0] => 1 [1] => Array ([0] => complex [1] = quaternion ))

The syntax of the include_once () statement is similar to that of the include () statement. The main difference is to avoid repeated definitions of functions or variables caused by multiple inclusion of a file.

The require_once statement has a reference chain, which ensures that the file is added to your program only once and avoids conflicts between variable values and function names.

Like the require_once statement, the include_once statement extends the include function. During program execution, the specified file is included. if the program referenced from the file has previously been included, include_once () will not include it again. That is, you can only reference the same file once!

The include_once () statement contains and runs the specified file during script execution. This behavior is similar to the include () statement. The only difference is that if the code in the file has been included, it will not be included again. As the statement name implies, it will only be included once.

Include_once () should be used when the same file may be contained more than once during script execution, to ensure that it is only included once to avoid function redefinition, variable re-assignment and other issues.

For more examples of using require_once () and include_once (), see The PEAR code in the latest PHP source program release package.

The returned value is the same as include. If the file has been included, this function returns TRUE.

Note: include_once () is newly added in PHP 4.0.1pl2.

Note: the behavior of include_once () and require_once () in case-insensitive operating systems (such as Windows) should be noted.

It may not be expected.
Example :??? Include_once () is case insensitive in Windows

Include_once ("a. php"); // this will include a. php
Include_once ("A. php"); // this will include a. php again on Windows! (PHP 4 only)
?>

This behavior is changed in PHP 5, and the path is normalized first, so C: \ PROGRA ~ The implementation of 1 \ A. php is the same as that of C: \ Program Files \ a. php, and the file will only be included once.

If the file to be included does not exist, include prompts notice, and then continues to execute the following statement. require prompts a fatal error and exits.

On the win32 Platform, they are all included first and then executed. Therefore, it is best not to have include or require statements in the contained files, which will cause directory confusion. It may be different in Linux and has not been tested yet.

If you do not want to include a file multiple times, you can use include_once or require_once # to read the file data.
Function r ($ file_name ){
$ Filenum = @ fopen ($ file_name, "r ");
@ Flock ($ filenum, LOCK_SH );
$ File_data = @ fread ($ filenum, filesize ($ file_name ));
@ Fclose ($ filenum );
Return $ file_data;
}
Function w ($ file_name, $ data, $ method = "w "){
$ Filenum = @ fopen ($ file_name, $ method );
Flock ($ filenum, LOCK_EX );
$ File_data = fwrite ($ filenum, $ data );
Fclose ($ filenum );
Return $ file_data;
}
?>

?

If necessary, let's take a look.

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.