Require ()
The require () statement includes and runs the specified file.
The require () statement includes and runs the specified file. For details about how to work, see the include () document.
Require () and include () are identical in all aspects except how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when the file is lost, do not hesitate to use require. This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set.
Example 16-2. Basic require () Example
<? Php tutorial
Require 'prepend. php ';
Require $ somefile;
Require ('somefile.txt ');
?>
For more examples, see the include () document.
Note: Before php 4.0.2, the following rules apply: require () always tries to read the target file, even if its row is not executed at all. The condition statement does not affect require (). However, if the line where require () is located is not executed, the code in the target file will not be executed. Similarly, the loop structure does not affect require () behavior. Although the Code contained in the target file is still the subject of a loop, require () itself runs only once.
Note: because this is a language structure rather than a function, it cannot be called by the variable function.
Include ()
The include () statement includes and runs the specified file.
The following documents also apply to require (). These two structures are identical except for how to handle failures. Include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (). This is not the case with include (). The script will continue to run. Make sure that the appropriate include_path is set.
When a file is included, the Code contained in the file inherits the variable range of the row where the include is located. From this point on, any variables available to the calling file in this row are also available in the called file.
Example 16-3. Basic include () Example
Vars. php
<? Php
$ Color = 'green ';
$ Fruit = 'apple ';
?>
Test. php
<? Php
Echo "a $ color $ fruit"; //
Include 'vars. php ';
Echo "a $ color $ fruit"; // a green apple
?>
If include appears in a function in the calling file, all the code contained in the called file will behave as they are defined inside the function. So it will follow the variable range of the function.
Example 16-4. functions include
<? Php
Function foo ()
{
Global $ color;
Include 'vars. php ';
Echo "a $ color $ fruit ";
}
/* Vars. php is in the scope of foo () so *
* $ Fruit is not available outside of this *
* Scope. $ color is because we declared it *
* As global .*/
Foo (); // a green apple
Echo "a $ color $ fruit"; // a green
?>
When a file is included, the syntax parser disconnects from the php mode at the beginning of the target file and enters the html mode to restore it at the end of the file. For this reason, any code that should be executed as php code in the target file must be included in the valid php start and end tags.
If "url fopen wrappers" is activated in php (default configuration), you can use url (via http or other supported encapsulation protocols-supported protocols are shown in appendix l) instead of a local file to specify the file to be included. If the target server interprets the target file as php code, you can use the url request string applicable to http get to pass variables to the included file. Strictly speaking, this is not the same as the variable space that includes a file and inherits the parent file. The script file is actually running on a remote server, while the local script includes the result.
Warning
Windows php versions earlier than version 4.3.0 do not support remote file access to this function, even if the allow_url_fopen option is activated.
Example 16-5. include () through http ()
<? Php
/* This example assumes that www.zhutiai.com is configured to parse. php *
* Files and not. txt files. also, 'works' here means that the variables *
* $ Foo and $ bar are available within the specified ded file .*/
// Won't work; file.txt wasn' t handled by www.example.com as php
Include 'HTTP: // www.example.com/file.txt? Foo = 1 & bar = 2 ';
// Won't work; looks for a file named 'file. php? Foo = 1 & bar = 2' on
// Local filesystem.
Include 'file. php? Foo = 1 & bar = 2 ';
// Works.
Include 'HTTP: // www.example.com/file.php? Foo = 1 & bar = 2 ';
$ Foo = 1;
$ Bar = 2;
Include 'file.txt '; // works.
Include 'file. php'; // works.
?>
For more information, see use remote files, fopen () and file ().
Because include () and require () are special language structures, they must be placed in the statement group (in curly brackets) in condition statements ).
Example 16-6. include () and Condition Statement Group
<? Php
// This is wrong and will not work as desired.
If ($ condition)
Include $ file;
Else
Include $ other;
// This is correct.
If ($ condition ){
Include $ file;
} Else {
Include $ other;
}
?>
Processing the return value: You can use the return () Statement in the included file to terminate the execution of the program in the file and return the script that calls it. You can also return values from included files. The Return Value of the include call can be obtained just like that of a common function.
Note: in php 3, return cannot appear in included files unless called in functions. In this case, return () acts on this function rather than the entire file.
Example 16-7. include () and return () Statements
Return. php
<? Php
$ Var = 'php ';
Return $ var;
?>
Noreturn. php
<? Php
$ Var = 'php ';
?>
Testreturns. php
<? Php
$ Foo = include 'Return. php ';
Echo $ foo; // prints 'php'
$ Bar = include 'noreturn. php ';
Echo $ bar; // prints 1
?>
The value of $ bar is 1 because the include operation is successful. Note the differences in the above example. The first one uses return () in the included file but the other does not. The other methods to "include" files to variables are to use fopen (), file (), or include () together with the output control function.
Note: because this is a language structure rather than a function, it cannot be called by the variable function.
Require_once ()
The require_once () statement includes and runs the specified file during script execution. This behavior is similar to the require () statement. The only difference is that if the code in the file has been included, it will not be included again. For how this statement works, see the require () documentation.
Require_once () should be used when the same file may be included more than once during script execution. You want to ensure that it is included only once to avoid function redefinition, variable re-assignment and other issues.
For examples of using require_once () and include_once (), see the pear code in the latest php source code release package.
Note: require_once () is newly added in php 4.0.1pl2.
Note: The behaviors of require_once () and include_once () in case-insensitive operating systems (such as windows) may not be what you expected. Example 16-8. require_once () is case insensitive in windows
<? Php
Require_once ("a. php"); // this will include a. php
Require_once ("a. php"); // this will include a. php again on windows!
?>
Warning
Windows php versions earlier than version 4.3.0 do not support remote file access to this function, even if the allow_url_fopen option is activated.
Include_once ()
The include_once () statement includes 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 included more than once during script execution. You want to ensure that it is included only 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.
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) may not be what you expected. Example 16-9. include_once () is case insensitive in windows
<? Php
Include_once ("a. php"); // this will include a. php
Include_once ("a. php"); // this will include a. php again on windows!
?>
Warning
Windows php versions earlier than version 4.3.0 do not support remote file access to this function, even if the allow_url_fopen option is activated.