Nclude ()
The include () statement includes and runs the specified file.
The following documents also apply to require (). These two structures are exactly the same in addition to how they handle failures. The include () generates a warning and require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (). The include () is not the case and the script will continue to run. Also be sure to set the appropriate include_path.
When a file is included, the code contained in it inherits the scope of the variable for the row in which the include is located. From there, any variables that are available to the calling file at that line are also available in the called file.
example 12-3. Basic include () example
vars.php
Copy CodeThe code is as follows:
$color = ' green ';
$fruit = ' Apple ';
?>
test.php
Copy CodeThe code is as follows:
echo "A $color $fruit"; A
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 if they were defined inside the function. So it will follow the variable range of the function.
example 12-4. Included in the function
Copy CodeThe code is as follows:
function foo ()
{
Global $color;
Include ' vars.php ';
echo "A $color $fruit";
}
/* vars.php is in the scope of foo () so *
* $fruit isn't 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 parser goes out of PHP mode at the beginning of the target file and enters HTML mode and resumes at the end of the file. For this reason, any code in the target file that should be executed as PHP code must be included in the valid PHP start and end tags.
If URL fopen wrappers is activated in PHP (the default configuration), you can specify which files to include by using URLs (via HTTP) instead of local files. If the destination server interprets the destination file as PHP code, you can pass the variable to the included file with the URL request string for the HTTP GET. Strictly speaking, this is not the same as including a file and inheriting the variable space of the parent file; The script file is actually already running on the remote server, and the local script includes its results.
Warning
The Windows version of PHP does not currently support remote file access for this function, even though the Allow_url_fopen option has been activated.
example 12-5. Include () via HTTP
Copy CodeThe code is as follows:
/* This example assumes, www.example.com is configured to parse. PHP *
* files and not. txt files. Also, ' Works ' here means, the variables *
* $foo and $bar is available within the included 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 the
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.
?>
See Using remote files, fopen () and file () for related information.
Because include () and require () are special language constructs, they must be placed in a statement group (in curly braces) in a conditional statement.
example 12-6. Include () and conditional statement groups
Copy CodeThe code is as follows:
This is wrong and would not be work as desired.
if ($condition)
Include $file;
Else
Include $other;
This is CORRECT.
if ($condition) {
Include $file;
} else {
Include $other;
}
?>
To process 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 called it. It is also possible to return values from the included files. You can get the return value of the include call like a normal function.
Note: in PHP 3, return cannot appear in files that are included unless called in a function. In this case, return () acts on the function instead of the entire file.
example 12-7. Include () and return () statements
return.php
Copy CodeThe code is as follows:
$var = ' PHP ';
return $var;
?>
noreturn.php
Copy CodeThe code is as follows:
$var = ' PHP ';
?>
testreturns.php
Copy CodeThe code is as follows:
$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 was successfully run. Note the differences in the above examples. The first one uses return () in the included file and the other does not. Several other ways to "include" the file into a variable are to use fopen (), file (), or include () together with the output control function.
http://www.bkjia.com/PHPjc/327653.html www.bkjia.com true http://www.bkjia.com/PHPjc/327653.html techarticle The nclude () The Include () statement includes and runs the specified file. The following documents also apply to require (). These two structures are exactly the same in addition to how they handle failures. Include () generates a police ...