Require (), include (), require_once () and include_once () difference

Source: Internet
Author: User
Tags flock parse error

Article reproduced from: http://www.jb51.net/article/13968.htm

I put these two statements together to introduce, the reader can compare study.
1.require () statement
The Require () statement is used to specify a file in place of the statement itself, just as the include () statement in C language. If the URL fopen wrappers in the PHP profile php.ini is open (which is open by default), you can use the URL to specify the location of the file to implement the call to the remote file.
One thing is to pay special attention to using the require () and include () statements. That is, in the included file, the processor interprets the content in HTML mode, and then returns to the PHP mode after processing the included content. So if you need to use the PHP syntax in the included file, use the correct PHP start and end tags to include the statements.
Require () and include () knowledge of a language feature in PHP, not a function. They and functions have many different places.
For example, a file contained in require () cannot contain a control structure and cannot use a statement such as return. Using the return statement in a file contained in require () can result in a processing error.
Unlike the include () statement, the Require () statement reads unconditionally the contents of the file it contains, regardless of whether the statements are executed or not. So if you want to include different files according to different criteria, you must use the Include () statement. Of course, if the statement in the location of require () is not executed, the statements in the file contained by require () will not be executed.
Require () cannot contain different files in the loop body depending on the condition. The Require () statement only invokes the contents of the file contained in it at the first execution to replace the statement itself, which can only execute the first contained statement when executed again. 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 files contained in the Require () statement. If the require () statement is inside a function, the statements within the contained file are equivalent to the definition within the function.
The Require () statement reads the file referenced by the require 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 is a bit strong, 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 to include in a conditional control statement, the reference file will be included if that condition is not true. Zombies are formed, and these zombies do not have any visible effect during the operation, but it is obvious that it will add to the burden, so pay special attention to this. If a include error occurs with the Require statement, the program prints the 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 according to the PHP code, the content contained in the local PHP file is processed on the remote server for subsequent results. For example:
/*
This example assumes that the Some_server server can interpret the. php file and not the. txt file. In the remote file
Variable $varfirst and $varsecond are required
*/
/* Cannot execute correctly, remote server does not process. txt file/
Require ("http://some_server/file.txt?varfirst=1&varsecond=2");

/* Incorrect, so you can only look for file.php files on the local machine * *
Require ("file.php?varfirst=1&varsecond=2");

/* The correct statement * *
Require ("http://some_server/file.php?varfirst=1&varsecond=2");

$varfirst = 1;
$varsecond = 2;
Require ("file.txt"); /* The correct statement * *
Require ("file.php"); /* The correct statement * *
In php3.0, require () contains files that can use the return statement, provided that the return statement cannot appear inside {}, but must appear in the global scope of the included 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 functionality of the require () statement is fully applicable to the include () statement, where the require () statement does not explicitly describe the part that does not apply to include (). The features and features of the include () statement that are not in the require () statement are described below.
The include statement reads the file to be included only when it is executed. Convenient for error handling, use the Include statement, and if a include error occurs, the program skips the include statement, although an error message is displayed but the program continues to execute.
The PHP processor will process it every time the include () statement is encountered, so you can use include () in conditional control statements and circular statements to contain different files, depending on the situation.
For example:
<?php
$files =array (' first.php ', ' second.php ', ' third.php ');
for ($i =0; $i <count ($files); $i + +)
{
Include $files [$i];
}
?>
You can use the return statement in the files contained in the include () statement in php3.0 and php4.0 for a value to be returned, and to stop executing the content below the included file. But php3.0 and php4.0 are different when dealing with such situations. In php3.0, the return statement cannot be contained within {} unless it is in a function because it represents a function's returned value instead of a file's return value. 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

Errors are often reported in php3.0. The following examples illustrate:
Suppose the included file is Test.inc and the main file main.php is in a directory. The contents of Test.inc are as follows:
Test.inc
<?php
echo "Before the return<br>/n";
if (1)
{
Return 27;
}
echo "After the return<br>/n";
?>

Suppose the following statement is included in the main.php file:
<?php
$retval =include (' test.inc ');
echo "File returned: ' $retval ' <br>/n ';
?>
The PHP3.0 interpreter will report an error in the second row and not get the return value of the include () statement. But in php4.0, you get the following results:
Before the return
File returned: ' 27 '
The bottom assumption main.php to:
<?php
Include (' Test.inc ');
echo "Back in main.html<br>/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 {} and leave it at the outermost end of the test.inc, the output is:
Before the return
27Back in main.html
27 occurs 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 want to include multiple files, and you can effectively avoid the error of repeating a function or variable definition by including the same piece of code. For example: If you create two files Util.inc and Fool.inc, the program code is:
Util.inc:
<?php
Define (Phpversion,floor (Phpversion ()));
echo "GLOBALS ARE nice<br>/n";
function Goodtea ()
{
Return to "Olong tea tasts good!";
}
?>
and Fool.inc:
<?php
Require ("Util.inc");
function Showvar ($var)
{
if (phpversion==4)
{
Print_r ($var);
}
Else
{
Var_dump ($var);
}
}
?>
The two files are then included in the error_require.php:
<?php
Require ("Fool.inc");
Require ("Util.inc");/This sentence will produce an error
$foo =array ("1", Array ("complex", "quaternion"));
echo "This is requiring util.inc again which is also<br>/n";
echo "required in fool.inc/n";
echo "Running Goodtea:". Goodtea (). <br>/n ";
echo "Printing foo:<br>/n";
Showvar ($foo);
?>
When you run error_require.php, the output is as follows:
GLOBALS ARE Nice
GLOBALS ARE Nice

Fatal error:cannot redeclare goodtea () in Util.inc to line 4

If you use the require_once () statement instead of the require () statement, the above error does not occur. We change the require () statement in error_require.php and Fool.inc to require_once () and rename it to error_require_once.php, which is the result of the display 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 the include () statement, and the main difference is to avoid repeating the definition of a function or variable by including a file multiple times.

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

As with the require_once statement, the Include_once statement extends the functionality of the include. During the execution of the program, the specified file is included, and include_once () does not include it if the program from which the file is referenced has previously been included. That is, you can only reference the same file once.

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

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

Use the require_once () and include_once () for more examples to see the PEAR code in the latest PHP source program release package.

The return value is the same as include (). This function returns TRUE if the file is already contained.

Note: include_once () is a new addition in the PHP 4.0.1pl2.

Note: Note that the behavior of include_once () and require_once () in the case insensitive operating system (for example, Windows)

Might not have been expected.
Example: Include_once () is not case-sensitive under Windows

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

This behavior is changed in PHP 5, the path is normalized first, so c:/progra~1/a.php and C:/Program files/a.php are implemented, and files are only included once.

If the file you want to include does not exist, include prompts notice, and then proceed to the following statement, require prompt for a fatal error and exit.

Under the Win32 platform they are all included and executed first, so it is best not to have include or require statements in the included files, which can cause directory clutter. Perhaps the situation is different under Linux, not yet tested.

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

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.