include

(PHP 4, PHP 5, PHP 7)

include 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷指锟斤拷锟侥硷拷锟斤拷

锟斤拷锟斤拷锟侥碉拷也锟斤拷锟斤拷锟斤拷 require锟斤拷

锟斤拷锟斤拷锟斤拷锟侥硷拷锟饺帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷路锟斤拷寻锟揭o拷锟斤拷锟矫伙拷懈锟斤拷锟侥柯硷拷锟街伙拷锟斤拷募锟斤拷锟斤拷锟绞憋拷锟斤拷锟� include_path 指锟斤拷锟斤拷目录寻锟揭★拷锟斤拷锟斤拷锟� include_path 锟斤拷没锟揭碉拷锟斤拷锟侥硷拷锟斤拷 include 锟斤拷锟斤拷锟节碉拷锟矫脚憋拷锟侥硷拷锟斤拷锟节碉拷目录锟酵碉拷前锟斤拷锟斤拷目录锟斤拷寻锟揭★拷锟斤拷锟斤拷锟斤拷锟斤拷未锟揭碉拷锟侥硷拷锟斤拷 include 锟结构锟结发锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟� require 锟斤拷同锟斤拷锟斤拷锟竭会发锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷锟铰凤拷锟�——锟斤拷锟斤拷锟角撅拷锟斤拷路锟斤拷锟斤拷锟斤拷 Windows 锟斤拷锟斤拷锟教凤拷锟斤拷锟斤拷 \ 锟斤拷头锟斤拷锟斤拷 Unix/Linux 锟斤拷锟斤拷 / 锟斤拷头锟斤拷锟斤拷锟角碉拷前目录锟斤拷锟斤拷锟铰凤拷锟斤拷锟斤拷锟� . 锟斤拷锟斤拷 .. 锟斤拷头锟斤拷——include_path 锟斤拷锟结被锟斤拷全锟斤拷锟皆★拷锟斤拷锟斤拷一锟斤拷锟侥硷拷锟斤拷 ../ 锟斤拷头锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诘锟角澳柯硷拷母锟侥柯硷拷锟窖帮拷腋锟斤拷募锟斤拷锟�

锟叫癸拷 PHP 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷募锟斤拷桶锟斤拷锟铰凤拷锟斤拷母锟斤拷锟斤拷锟较拷渭锟� include_path 锟斤拷锟街碉拷锟侥碉拷锟斤拷

锟斤拷一锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥达拷锟斤拷坛锟斤拷锟� include 锟斤拷锟斤拷锟叫碉拷锟斤拷锟斤拷锟斤拷围锟斤拷锟接该达拷锟斤拷始锟斤拷锟斤拷锟斤拷锟侥硷拷锟节革拷锟叫达拷锟斤拷锟矫碉拷锟轿何憋拷锟斤拷锟节憋拷锟斤拷锟矫碉拷锟侥硷拷锟斤拷也锟斤拷锟斤拷锟矫★拷锟斤拷锟斤拷锟斤拷锟斤拷锟节帮拷锟斤拷锟侥硷拷锟叫讹拷锟斤拷暮锟斤拷锟斤拷锟斤拷喽硷拷锟斤拷锟饺拷锟斤拷锟斤拷锟斤拷锟�

Example #1 锟斤拷锟斤拷锟斤拷 include 锟斤拷锟斤拷

vars.php
<?php

$color 
'green';
$fruit 'apple';

?>

test.php
<?php

echo "A $color $fruit"// A

include 'vars.php';

echo 
"A $color $fruit"// A green apple

?>

锟斤拷锟� include 锟斤拷锟斤拷锟节碉拷锟斤拷锟侥硷拷锟叫碉拷一锟斤拷锟斤拷锟斤拷锟斤,锟津被碉拷锟矫碉拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫达拷锟诫将锟斤拷锟街碉拷锟斤拷同锟斤拷锟斤拷锟斤拷锟节该猴拷锟斤拷锟节诧拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟窖拷煤锟斤拷锟斤拷谋锟斤拷锟斤拷锟轿э拷锟斤拷斯锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷魔锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节凤拷锟斤拷锟斤拷锟斤拷之前锟斤拷锟窖憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷摹锟�

Example #2 锟斤拷锟斤拷锟叫的帮拷锟斤拷

<?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

?>

锟斤拷一锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤法锟斤拷锟斤拷锟斤拷锟斤拷目锟斤拷锟侥硷拷锟侥匡拷头锟斤拷锟斤拷 PHP 模式锟斤拷锟斤拷锟斤拷 HTML 模式锟斤拷锟斤拷锟侥硷拷锟斤拷尾锟斤拷锟街革拷锟斤拷锟斤拷锟节达拷原锟斤拷目锟斤拷锟侥硷拷锟斤拷锟斤拷要锟斤拷为 PHP 锟斤拷锟斤拷执锟叫碉拷锟轿何达拷锟诫都锟斤拷锟诫被锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷 PHP 锟斤拷始锟酵斤拷锟斤拷锟斤拷锟�之锟叫★拷

锟斤拷锟�"URL fopen wrappers"锟斤拷 PHP 锟叫憋拷锟斤拷锟筋(默锟斤拷锟斤拷锟矫o拷锟斤拷锟斤拷锟斤拷锟斤拷 URL锟斤拷通锟斤拷 HTTP 锟斤拷锟斤拷锟斤拷锟斤拷支锟街的凤拷装协锟斤拷——锟斤拷支锟街碉拷协锟斤拷头锟阶靶拷锟�锟斤拷锟斤拷锟斤拷锟角憋拷锟斤拷锟侥硷拷锟斤拷指锟斤拷要锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟侥匡拷锟斤拷锟斤拷锟斤拷锟斤拷目锟斤拷锟侥硷拷锟斤拷为 PHP 锟斤拷锟斤拷锟斤拷停锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 HTTP GET 锟斤拷 URL 锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟津被帮拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟捷憋拷锟斤拷锟斤拷锟较革拷锟剿碉拷锟酵帮拷锟斤拷一锟斤拷锟侥硷拷锟斤拷锟教承革拷锟侥硷拷锟侥憋拷锟斤拷锟秸间并锟斤拷锟斤拷一锟斤拷锟铰o拷锟矫脚憋拷锟侥硷拷实锟斤拷锟斤拷锟窖撅拷锟斤拷远锟教凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿o拷锟斤拷锟斤拷锟截脚憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

Warning

Windows 锟芥本锟斤拷 PHP 锟斤拷 4.3.0 锟斤拷之前锟斤拷支锟斤拷通锟斤拷锟剿猴拷锟斤拷锟斤拷锟斤拷远锟斤拷锟侥硷拷锟斤拷锟斤拷使锟窖撅拷锟斤拷锟斤拷 allow_url_fopen.

Example #3 通锟斤拷 HTTP 锟斤拷锟叫碉拷 include

<?php

/* This example assumes that www.example.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 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.

?>

Warning

锟斤拷全锟斤拷锟斤拷

远锟斤拷锟侥硷拷锟斤拷锟杰会经远锟教凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷缀锟皆硷拷远锟教凤拷锟斤拷锟斤拷锟角凤拷锟斤拷锟斤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷戏锟斤拷锟� PHP 锟脚憋拷锟斤拷锟斤拷为锟戒将锟斤拷锟斤拷锟截凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆讹拷谭锟斤拷锟斤拷锟斤拷锟斤拷募锟接︼拷锟斤拷锟皆讹拷锟斤拷锟斤拷卸锟街伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� readfile() 锟斤拷锟斤拷锟斤拷锟矫★拷锟斤拷锟解还要锟斤拷锟斤拷小锟斤拷锟斤拷确锟斤拷远锟教的脚憋拷锟斤拷锟斤拷锟斤拷锟较凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷拇锟斤拷搿�

锟斤拷锟斤拷锟较拷渭锟�使锟斤拷远锟斤拷锟侥硷拷锟斤拷fopen() 锟斤拷 file()锟斤拷

锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷失锟斤拷时 include 锟斤拷锟斤拷 FALSE 锟斤拷锟揭凤拷锟斤拷锟斤拷锟芥。锟缴癸拷锟侥帮拷锟斤拷锟津返伙拷 1锟斤拷锟斤拷锟斤拷锟节帮拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷朔锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷诒锟斤拷锟斤拷锟斤拷锟斤拷募锟斤拷锟绞癸拷锟� return 锟斤拷锟斤拷锟斤拷锟街癸拷锟斤拷募锟斤拷谐锟斤拷锟斤拷执锟叫诧拷锟斤拷锟截碉拷锟斤拷锟斤拷锟侥脚憋拷锟斤拷同锟斤拷也锟斤拷锟皆从憋拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟叫凤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷一锟斤拷锟斤拷锟� include 锟斤拷锟矫的凤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟节帮拷锟斤拷远锟斤拷锟侥硷拷时却锟斤拷锟叫o拷锟斤拷锟斤拷远锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟较凤拷锟斤拷 PHP 锟斤拷始锟酵斤拷锟斤拷锟斤拷锟�锟斤拷锟斤拷同锟轿何憋拷锟斤拷锟侥硷拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节憋拷锟斤拷诙锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟矫憋拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷位锟斤拷之锟斤拷涂锟斤拷锟斤拷恕锟�

锟斤拷为 include 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷越峁癸拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟脚★拷锟节比斤拷锟戒返锟斤拷值时要注锟解。

Example #4 锟饺斤拷 include 锟侥凤拷锟斤拷值

<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 
'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 
'OK';
}
?>

Example #5 include 锟斤拷 return 锟斤拷锟�

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

?>

$bar 锟斤拷值为 1 锟斤拷锟斤拷为 include 锟缴癸拷锟斤拷锟斤拷锟剿★拷注锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟金。碉拷一锟斤拷锟节憋拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷 return 锟斤拷锟斤拷一锟斤拷没锟叫★拷锟斤拷锟斤拷募锟斤拷锟斤拷鼙锟斤拷锟斤拷锟斤拷锟斤拷蚍祷锟� FALSE 锟斤拷锟斤拷锟斤拷一锟斤拷 E_WARNING 锟斤拷锟芥。

锟斤拷锟斤拷诎锟斤拷锟斤拷募锟斤拷卸锟斤拷锟斤拷泻锟斤拷锟斤拷锟斤拷锟叫╋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� return 之前锟斤拷锟斤拷之锟斤拷锟斤拷模锟斤拷锟斤拷锟斤拷远锟斤拷锟斤拷锟斤拷锟斤拷募锟斤拷锟绞癸拷谩锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿o拷PHP 5 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟窖撅拷锟斤拷锟斤拷锟藉,锟斤拷锟斤拷 PHP 4 锟斤拷锟斤拷锟斤拷锟� return 之锟斤拷锟斤拷暮锟斤拷锟斤拷锟斤拷锟斤拷萍锟绞癸拷锟� include_once 锟斤拷锟斤拷锟角硷拷锟斤拷募锟斤拷欠锟斤拷寻锟斤拷锟斤拷锟斤拷诎锟斤拷锟斤拷募锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷亍锟�

锟斤拷一锟斤拷锟斤拷 PHP 锟侥硷拷"锟斤拷锟斤拷"锟斤拷一锟斤拷锟斤拷锟斤拷锟叫的凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷坪锟斤拷锟�锟斤拷锟� include 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷纾�

Example #6 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� PHP 锟侥硷拷锟斤拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷

<?php
$string 
get_include_contents('somefile.php');

function 
get_include_contents($filename) {
    if (
is_file($filename)) {
        
ob_start();
        include 
$filename;
        
$contents ob_get_contents();
        
ob_end_clean();
        return 
$contents;
    }
    return 
false;
}

?>

要锟节脚憋拷锟斤拷锟皆讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟轿硷拷 php.ini 锟叫碉拷 auto_prepend_file 锟斤拷 auto_append_file 锟斤拷锟斤拷选锟筋。

Note: 锟斤拷为锟斤拷一锟斤拷锟斤拷锟皆癸拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟杰憋拷 锟缴变函锟斤拷 锟斤拷锟矫★拷

锟轿硷拷 require锟斤拷require_once锟斤拷include_once锟斤拷get_included_files()锟斤拷readfile()锟斤拷virtual() 锟斤拷 include_path锟斤拷