Example 12-5. include() over HTTP
// Won't work; file.txt wasn't handled by www.example.com as PHPinclude 'http://www.example.com/file.txt?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 related information, see Using remote files, fopen() and file().
Because include() and require() are special language constructs, they must be placed in a statement group (in curly braces) when used in conditional statements.
Example 12-6. include() and conditional statement group
Copy code
The code is as follows:
// 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;
}
?>
Handling return values: You can use the return() statement in an included file to terminate the execution of the program in the file and return to the script that called it. It is also possible to return values from included files. The return value of an include call can be obtained like a normal function.
Note: In PHP 3, return cannot appear in included files unless called in a function. In this case return() acts on the function rather than the entire file.
Example 12-7. include() and return() statements
return.php
Copy Code The code is as follows:
$var = 'PHP';
return $var;
?>
noreturn.php
Copy code The code is as follows:
$var = 'PHP';
?>
testreturns.php
Copy code The 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 include ran successfully. Note the difference in the above examples. The first uses return() in the included file and the other does not. Several other ways to "include" files into variables are to use fopen(), file() or include() in conjunction with output control functions.
http://www.bkjia.com/PHPjc/327653.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327653.htmlTechArticlenclude() The include() statement includes and runs the specified file. The following documentation also applies to require(). The two structures are identical except for how they handle failure. include() generates an alert...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn