Home  >  Article  >  Backend Development  >  Detailed explanation of include() usage techniques in php

Detailed explanation of include() usage techniques in php

黄舟
黄舟Original
2017-06-25 11:05:313926browse

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() produces a warning and require() results in a fatal error. In other words, use require() if you want to stop processing the page if a missing file is encountered. This is not the case with include() and the script will continue to run. Also make sure the appropriate include_path is set.

When a file is included, the code contained in it inherits the variable scope of the line where the include is located. From that point on, any variables available in the calling file at that line are also available in the called file.

Basic include() example

vars.php
<?php
$color = &#39;green&#39;;
$fruit = &#39;apple&#39;;
?>
test.php
<?php
echo "A $color $fruit"; // A
include &#39;vars.php&#39;;
echo "A $color $fruit"; // A green apple
?>

If include appears in a function in the calling file, all code contained in the called file will behave as if they were in that function. The same as defined inside the function. So it will follow the variable scope of that function.

Includes in functions

<?php
function foo()
{
   global $color;
   include &#39;vars.php&#39;;
   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 parser leaves PHP mode and enters HTML mode at the beginning of the target file, to Restore at end of file. For this reason, any code in an object file that should be executed as PHP code must be included within valid PHP start and end tags.

If "URL fopen wrappers" are enabled in PHP (the default configuration), you can specify the request using a URL (via HTTP or other supported wrapping protocols - see Appendix L for supported protocols) instead of a local file. Included files. If the target server interprets the target file as PHP code, you can pass variables to the included file using the URL requeststring for HTTP GET. Strictly speaking, this is not the same thing as including a file and inheriting the variable space of the parent file; the script file is actually run on the remote server, and the local script includes its results.

WARNING

Windows versions of PHP prior to version 4.3.0 do not support remote file access for this function, even if the allow_url_fopen option is activated.

include() over HTTP

<?php
/* This example assumes that www.example.com is configured to parse .php *
 * files and not .txt files. Also, &#39;Works&#39; here means that the variables *
 * $foo and $bar are available within the included file.                */
// Won&#39;t work; file.txt wasn&#39;t handled by www.example.com as PHP
include &#39;http://www.example.com/file.txt?foo=1&bar=2&#39;;
// Won&#39;t work; looks for a file named &#39;file.php?foo=1&bar=2&#39; on the
// local filesystem.
include &#39;file.php?foo=1&bar=2&#39;;
// Works.
include &#39;http://www.example.com/file.php?foo=1&bar=2&#39;;
$foo = 1;
$bar = 2;
include &#39;file.txt&#39;;  // Works.
include &#39;file.php&#39;;  // Works.
?>

For information, see Using remote files, fopen() and file().

Because include() and require() are special language structures, they must be placed in a statement group (in curly braces) when used in conditional statements.

include() and conditional 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 return values: You can use the return() statement in an included file to terminate the file The execution of the program and returns the script that called it. It is also possible to return values ​​from included files. The return value of the 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.

include() and return() statements

return.php
<?php
$var = &#39;PHP&#39;;
return $var;
?>
noreturn.php
<?php
$var = &#39;PHP&#39;;
?>
testreturns.php
<?php
$foo = include &#39;return.php&#39;;
echo $foo; // prints &#39;PHP&#39;
$bar = include &#39;noreturn.php&#39;;
echo $bar; // prints 1
?>


$bar The value is 1 because include ran successfully. Notice the difference in the examples above. The first uses return() in the included file and the other does not. Several other ways to "include" a file into a variable are to use fopen(), file(), or include() in conjunction with an output control function.

Note: Since this is a language structure rather than a function, it cannot be called by "variable functions".

The above is the detailed content of Detailed explanation of include() usage techniques in php. For more information, please follow other related articles on the PHP Chinese website!

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