Include and require in PHP

Source: Internet
Author: User
Tags flock parse error
Include and require in PHP

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

?

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

There are many similarities and differences between require () and include (). It is important to understand their differences, otherwise it is easy to make mistakes.

I put these two statements together to introduce, the reader can compare learning.
1.require () statement
???? The Require () statement is used for the specified file instead of the statement itself, just like the include () statement in the C language. If the URL fopen wrappers in php config file php.ini is open (by default), you can use the URL to specify the location of the file to make a call to the remote file.
???? One thing is to pay special attention when using the require () and include () statements. That is, in the included file, the processor interprets the content in HTML mode, and then reverts to PHP mode after processing the contained content. So if you need to use PHP syntax in the included file, use the correct PHP start and end tags to include the statements.
???? Require () and include () knowledge PHP is a language feature, not a function. They have many different places from functions.
For example: Require () contains a file that cannot contain a control structure and cannot use a statement such as return. Using the return statement in the files contained in require () will result in processing errors.
???? Unlike the include () statement, the Require () statement unconditionally reads the contents of the files it contains, regardless of whether the statements are executed. So if you want to include different files according to different conditions, you must use the Include () statement. Of course, if the statement at the location of require () is not executed, the statements in the file contained in require () will not be executed.
???? Require () cannot contain different files in the loop body depending on the condition. The Require () statement will only invoke the contents of the file it contains in the first execution and replace the statement itself, and when executed again, it can only execute the statement that was contained for the first time. However, the include () statement can contain different files in the loop body.
???? The variable in the Require () statement inherits the scope of the variable where the require () statement is located. All variables that can be accessed at the location of the require () statement can be accessed in the file contained in the Require () statement. If the require () statement is inside a function, the statements within the contained file are equivalent to those defined inside the function.
???? The Require () statement reads a file that uses require references before the PHP program executes, so require is usually placed at the beginning of the program. So pay special attention to the fact that the require statement has a bit of a strong, regardless of whether the program really needs to refer to the file, as long as you use the Require statement, it will include them in! Even if you are using this function for inclusion in conditional control statements, if that condition is not true, the reference file will be included! Zombies are formed, and these zombies do not play any visible role during the run, but it is obvious that it will add to the burden, so pay special attention to this! If an error is included with the Require statement, the program outputs an error message and stops running!!

???? If the Require () statement contains a remote file by declaring the URL of the file, and the remote server interprets the file as PHP code, the content contained in the local PHP file is processed later 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, require () contains files that can use the return statement, but the condition is that the return statement cannot appear inside the {} and must appear in the global scope of the contained file. This feature of require () has been canceled in php4.0, but can still be implemented using include ().

2.include () statement
???? The include () statement and the Require () statement have many of the same places. The function of the require () statement is fully applicable to the include () statement in the upper require () statement without explicitly stating that it cannot be applied to the include () section. Below is a description of the features and features of the include () statement that the require () statement does not have.
???? The include statement reads only the files to be included when it is executed. When error handling is convenient, the include statement is used, and if a containing error occurs, the program skips the include statement, although an error message is displayed but the program will continue to execute!
??? The PHP processor will re-process each time it encounters an include () statement, so you can use include () in conditional control statements and loop statements to contain different files, depending on the situation.
??? For example:
!--? php $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 contents of the included file. But php3.0 and php4.0 are different when dealing with such situations. The return statement in php3.0 cannot be contained within {} unless it is in a function because it represents the return value of the function instead of the return value of the file. There is no such restriction in php4.0, and users can even return a number in a file, just like the return value of a function. Such statements are in

Errors are usually reported in php3.0. The following examples illustrate:
??? Assume that the contained file is test.inc and the primary file main.php is located in a directory. The contents of Test.inc are as follows:
Test.inc
echo "Before the return"
\ n ";
if (1)
{
??????? Return 27;
}
echo "After the return"
\ n ";
?>

Assume that the following statement is included in the main.php file:
$retval =include (' test.inc ');
echo "File returned: ' $retval '
\ n ";
?>
???? The PHP3.0 interpreter reports an error on the second line and cannot get the return value of the include () statement. However, the following results will be obtained in the php4.0:
Before the return
File returned: ' 27 '
The following hypothesis main.php instead:
Include (' Test.inc ');
echo "Back in main.html
\ n ";
?>
The output in php4.0 is:
Before the return
Back in main.html

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

The output in php3.0 is:
Before the return
27Back 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 is not inside a function. If you remove {} so that it is at the outermost layer of test.inc, the output is:
Before the return
27Back in main.html
27 is present because the include () return 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 primarily used when you need to include multiple files, and you can effectively avoid errors where the same piece of code is included in a function or variable that is repeatedly defined. For example: If you create two files Util.inc and Fool.inc, the program code is:
Util.inc:
??? Define (Phpversion,floor (Phpversion ()));
??? echo "GLOBALS is 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);
??????????}
???}
?>
These two files are then included in the error_require.php:
Require ("Fool.inc");
Require ("Util.inc");//This sentence will produce an error
$foo =array ("1", Array ("complex", "quaternion"));
??????? echo "This was 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 is as follows:
???? GLOBALS is nice
???? GLOBALS is 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. Let's change the Require () statement in error_require.php and Fool.inc to the require_once () statement and rename it to error_require_once.php, which shows the following result:
GLOBALS is 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 the include () statement, and the main difference is to avoid repeating the definition of a function or variable by including a single file multiple times.

The require_once statement has a reference chain that guarantees that the file will be added to your program only once, and avoids collisions between the variable value and the function name.

As with require_once statements, the Include_once statement extends the functionality of include. During the execution of the program, the specified file is included, and include_once () will not include it if the program referenced from the file was previously included. That is, you can only refer to 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, except that if the code in the file is already contained, it will not be included again. As the name of this statement implies, it is included only once.

Include_once () should be used when the same file is likely to be included more than once during script execution, to ensure that it is included only once to avoid problems such as function redefinition, variable re-assignment, and so on.

See the PEAR code in the latest PHP source release package for more examples using require_once () and include_once ().

The return value is the same as include (). If the file is already contained, this function returns TRUE.

Note: include_once () is a new addition to PHP 4.0.1PL2.

Note: Be aware of the behavior of Include_once () and require_once () in case insensitive operating systems (such as Windows)

May not be what is expected.
Example:??? Include_once () is case insensitive under Windows

Include_once ("a.php"); This would include a.php
Include_once ("a.php"); This would include a.php again on windows! (PHP 4 only)
?>

This behavior is changed in PHP 5, the path is first normalized, so c:\progra~1\a.php and C:\Program files\a.php implementation, the file will only be included once.

If the file that you want to include does not exist, include prompts notice, and then continues with the following statement, require prompts for a fatal error and exits.

Win32 platform they are all first included after execution, so the included file is best not have include or require statements, which will cause the directory confusion. Maybe the Linux situation is different, not yet tested.

If a file does not want to be included multiple times can be read using include_once or require_once##, the document data can be written.
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;
}
?>

?

Leave it in detail and see if you need anything.

  • Related Article

    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.