PHP鎺у埗缁撴瀯涔媔nclude
鍐呭鎽樿
include() 璇彞鍖呭惈骞惰繍琛屾寚瀹氭枃浠躲
鏂囩珷姝f枃
include()
include() 璇彞鍖呭惈骞惰繍琛屾寚瀹氭枃浠躲
浠ヤ笅鏂囨。涔熼傜敤浜 require()銆傝繖涓ょ缁撴瀯闄や簡鍦ㄥ浣曞鐞嗗け璐ヤ箣澶栧畬鍏ㄤ竴鏍枫俰nclude() 浜х敓涓涓鍛婅 require() 鍒欏鑷翠竴涓嚧鍛介敊璇傛崲鍙ヨ瘽璇达紝濡傛灉鎯冲湪閬囧埌涓㈠け鏂囦欢鏃跺仠姝㈠鐞嗛〉闈㈠氨鐢 require()銆俰nclude() 灏变笉鏄繖鏍凤紝鑴氭湰浼氱户缁繍琛屻傚悓鏃朵篃瑕佺‘璁よ缃簡鍚堥傜殑 include_path銆傛敞鎰忓湪 PHP 4.3.5 涔嬪墠锛屽寘鍚枃浠朵腑鐨勮娉曢敊璇笉浼氬鑷寸▼搴忓仠姝紝浣嗕粠姝ょ増鏈箣鍚庝細銆
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in the current working directory. ++++ 瀵绘壘鍖呭惈鏂囦欢鐨勯『搴忓厛鏄湪褰撳墠宸ヤ綔鐩綍鐨勭浉瀵圭殑 include_path 涓嬪鎵撅紝鐒跺悗鏄綋鍓嶈繍琛岃剼鏈墍鍦ㄧ洰褰曠浉瀵圭殑 include_path 涓嬪鎵俱備緥濡 include_path 鏄 .锛屽綋鍓嶅伐浣滅洰褰曟槸 /www/锛岃剼鏈腑瑕 include 涓涓 include/a.php 骞朵笖鍦ㄨ鏂囦欢涓湁涓鍙 include "b.php"锛屽垯瀵绘壘 b.php 鐨勯『搴忓厛鏄 /www/锛岀劧鍚庢槸 /www/include/銆傚鏋滄枃浠跺悕浠 ./ 鎴栬 ../ 寮濮嬶紝鍒欏彧鍦ㄥ綋鍓嶅伐浣滅洰褰曠浉瀵圭殑 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 鍑虹幇浜庤皟鐢ㄦ枃浠朵腑鐨勪竴涓嚱鏁伴噷锛屽垯琚皟鐢ㄧ殑鏂囦欢涓墍鍖呭惈鐨勬墍鏈変唬鐮佸皢琛ㄧ幇寰楀鍚屽畠浠槸鍦ㄨ鍑芥暟鍐呴儴瀹氫箟鐨勪竴鏍枫傛墍浠ュ畠灏嗛伒寰鍑芥暟鐨勫彉閲忚寖鍥淬 An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
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
?>
褰撲竴涓枃浠惰鍖呭惈鏃讹紝璇硶瑙f瀽鍣ㄥ湪鐩爣鏂囦欢鐨勫紑澶磋劚绂 PHP 妯″紡骞惰繘鍏 HTML 妯″紡锛屽埌鏂囦欢缁撳熬澶勬仮澶嶃傜敱浜庢鍘熷洜锛岀洰鏍囨枃浠朵腑搴旇褰撲綔 PHP 浠g爜鎵ц鐨勪换浣曚唬鐮侀兘蹇呴』琚寘鎷湪鏈夋晥鐨 PHP 璧峰鍜岀粨鏉熸爣璁颁箣涓
濡傛灉鈥淯RL fopen wrappers鈥濆湪 PHP 涓婵娲伙紙榛樿閰嶇疆锛夛紝鍙互鐢 URL锛堥氳繃 HTTP 鎴栬呭叾瀹冩敮鎸佺殑灏佽鍗忚鈥斺旀墍鏀寔鐨勫崗璁Supported Protocols and Wrappers锛夎屼笉鏄湰鍦版枃浠舵潵鎸囧畾瑕佽鍖呭惈鐨勬枃浠躲傚鏋滅洰鏍囨湇鍔″櫒灏嗙洰鏍囨枃浠朵綔涓 PHP 浠g爜瑙i噴锛屽垯鍙互鐢ㄩ傜敤浜 HTTP GET 鐨 URL 璇锋眰瀛楃涓叉潵鍚戣鍖呮嫭鐨勬枃浠朵紶閫掑彉閲忋備弗鏍肩殑璇磋繖鍜屽寘鍚竴涓枃浠跺苟缁ф壙鐖舵枃浠剁殑鍙橀噺绌洪棿骞朵笉鏄竴鍥炰簨锛涜鑴氭湰鏂囦欢瀹為檯涓婂凡缁忓湪杩滅▼鏈嶅姟鍣ㄤ笂杩愯浜嗭紝鑰屾湰鍦拌剼鏈垯鍖呮嫭浜嗗叾缁撴灉銆
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() 鍑芥暟鏇村ソ銆傚彟澶栬繕瑕佹牸澶栧皬蹇冧互纭繚杩滅▼鐨勮剼鏈骇鐢熷嚭鍚堟硶骞朵笖鏄墍闇鐨勪唬鐮併
鐩稿叧淇℃伅鍙傝浣跨敤杩滅▼鏂囦欢锛宖open() 鍜 file()銆
澶勭悊杩斿洖鍊硷細鍙互鍦ㄨ鍖呮嫭鐨勬枃浠朵腑浣跨敤 return() 璇彞鏉ョ粓姝㈣鏂囦欢涓▼搴忕殑鎵ц骞惰繑鍥炶皟鐢ㄥ畠鐨勮剼鏈傚悓鏍蜂篃鍙互浠庤鍖呭惈鐨勬枃浠朵腑杩斿洖鍊笺傚彲浠ュ儚鏅氬嚱鏁颁竴鏍疯幏寰 include 璋冪敤鐨勮繑鍥炲笺備笉杩囪繖鍦ㄥ寘鍚繙绋嬫枃浠舵椂鍗翠笉琛岋紝闄ら潪杩滅▼鏂囦欢鐨勮緭鍑哄叿鏈夊悎娉曠殑 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() 涔嬪墠杩樻槸涔嬪悗鍦ㄤ富鏂囦欢涓娇鐢ㄣ傚鏋滄枃浠惰鍖呭惈涓ゆ锛孭HP 5 鍙戝嚭鑷村懡閿欒鍥犱负鍑芥暟宸茬粡琚畾涔夛紝浣嗘槸 PHP 鍦 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()锛宺equire_once()锛宨nclude_once()锛実et_included_files()锛宺eadfile()锛寁irtual() 鍜 include_path銆
include() 璇彞鍖呭惈骞惰繍琛屾寚瀹氭枃浠躲
浠ヤ笅鏂囨。涔熼傜敤浜 require()銆傝繖涓ょ缁撴瀯闄や簡鍦ㄥ浣曞鐞嗗け璐ヤ箣澶栧畬鍏ㄤ竴鏍枫俰nclude() 浜х敓涓涓鍛婅 require() 鍒欏鑷翠竴涓嚧鍛介敊璇傛崲鍙ヨ瘽璇达紝濡傛灉鎯冲湪閬囧埌涓㈠け鏂囦欢鏃跺仠姝㈠鐞嗛〉闈㈠氨鐢 require()銆俰nclude() 灏变笉鏄繖鏍凤紝鑴氭湰浼氱户缁繍琛屻傚悓鏃朵篃瑕佺‘璁よ缃簡鍚堥傜殑 include_path銆傛敞鎰忓湪 PHP 4.3.5 涔嬪墠锛屽寘鍚枃浠朵腑鐨勮娉曢敊璇笉浼氬鑷寸▼搴忓仠姝紝浣嗕粠姝ょ増鏈箣鍚庝細銆
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in the current working directory. ++++ 瀵绘壘鍖呭惈鏂囦欢鐨勯『搴忓厛鏄湪褰撳墠宸ヤ綔鐩綍鐨勭浉瀵圭殑 include_path 涓嬪鎵撅紝鐒跺悗鏄綋鍓嶈繍琛岃剼鏈墍鍦ㄧ洰褰曠浉瀵圭殑 include_path 涓嬪鎵俱備緥濡 include_path 鏄 .锛屽綋鍓嶅伐浣滅洰褰曟槸 /www/锛岃剼鏈腑瑕 include 涓涓 include/a.php 骞朵笖鍦ㄨ鏂囦欢涓湁涓鍙 include "b.php"锛屽垯瀵绘壘 b.php 鐨勯『搴忓厛鏄 /www/锛岀劧鍚庢槸 /www/include/銆傚鏋滄枃浠跺悕浠 ./ 鎴栬 ../ 寮濮嬶紝鍒欏彧鍦ㄥ綋鍓嶅伐浣滅洰褰曠浉瀵圭殑 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 鍑虹幇浜庤皟鐢ㄦ枃浠朵腑鐨勪竴涓嚱鏁伴噷锛屽垯琚皟鐢ㄧ殑鏂囦欢涓墍鍖呭惈鐨勬墍鏈変唬鐮佸皢琛ㄧ幇寰楀鍚屽畠浠槸鍦ㄨ鍑芥暟鍐呴儴瀹氫箟鐨勪竴鏍枫傛墍浠ュ畠灏嗛伒寰鍑芥暟鐨勫彉閲忚寖鍥淬 An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
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
?>
褰撲竴涓枃浠惰鍖呭惈鏃讹紝璇硶瑙f瀽鍣ㄥ湪鐩爣鏂囦欢鐨勫紑澶磋劚绂 PHP 妯″紡骞惰繘鍏 HTML 妯″紡锛屽埌鏂囦欢缁撳熬澶勬仮澶嶃傜敱浜庢鍘熷洜锛岀洰鏍囨枃浠朵腑搴旇褰撲綔 PHP 浠g爜鎵ц鐨勪换浣曚唬鐮侀兘蹇呴』琚寘鎷湪鏈夋晥鐨 PHP 璧峰鍜岀粨鏉熸爣璁颁箣涓
濡傛灉鈥淯RL fopen wrappers鈥濆湪 PHP 涓婵娲伙紙榛樿閰嶇疆锛夛紝鍙互鐢 URL锛堥氳繃 HTTP 鎴栬呭叾瀹冩敮鎸佺殑灏佽鍗忚鈥斺旀墍鏀寔鐨勫崗璁Supported Protocols and Wrappers锛夎屼笉鏄湰鍦版枃浠舵潵鎸囧畾瑕佽鍖呭惈鐨勬枃浠躲傚鏋滅洰鏍囨湇鍔″櫒灏嗙洰鏍囨枃浠朵綔涓 PHP 浠g爜瑙i噴锛屽垯鍙互鐢ㄩ傜敤浜 HTTP GET 鐨 URL 璇锋眰瀛楃涓叉潵鍚戣鍖呮嫭鐨勬枃浠朵紶閫掑彉閲忋備弗鏍肩殑璇磋繖鍜屽寘鍚竴涓枃浠跺苟缁ф壙鐖舵枃浠剁殑鍙橀噺绌洪棿骞朵笉鏄竴鍥炰簨锛涜鑴氭湰鏂囦欢瀹為檯涓婂凡缁忓湪杩滅▼鏈嶅姟鍣ㄤ笂杩愯浜嗭紝鑰屾湰鍦拌剼鏈垯鍖呮嫭浜嗗叾缁撴灉銆
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() 鍑芥暟鏇村ソ銆傚彟澶栬繕瑕佹牸澶栧皬蹇冧互纭繚杩滅▼鐨勮剼鏈骇鐢熷嚭鍚堟硶骞朵笖鏄墍闇鐨勪唬鐮併
鐩稿叧淇℃伅鍙傝浣跨敤杩滅▼鏂囦欢锛宖open() 鍜 file()銆
澶勭悊杩斿洖鍊硷細鍙互鍦ㄨ鍖呮嫭鐨勬枃浠朵腑浣跨敤 return() 璇彞鏉ョ粓姝㈣鏂囦欢涓▼搴忕殑鎵ц骞惰繑鍥炶皟鐢ㄥ畠鐨勮剼鏈傚悓鏍蜂篃鍙互浠庤鍖呭惈鐨勬枃浠朵腑杩斿洖鍊笺傚彲浠ュ儚鏅氬嚱鏁颁竴鏍疯幏寰 include 璋冪敤鐨勮繑鍥炲笺備笉杩囪繖鍦ㄥ寘鍚繙绋嬫枃浠舵椂鍗翠笉琛岋紝闄ら潪杩滅▼鏂囦欢鐨勮緭鍑哄叿鏈夊悎娉曠殑 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() 涔嬪墠杩樻槸涔嬪悗鍦ㄤ富鏂囦欢涓娇鐢ㄣ傚鏋滄枃浠惰鍖呭惈涓ゆ锛孭HP 5 鍙戝嚭鑷村懡閿欒鍥犱负鍑芥暟宸茬粡琚畾涔夛紝浣嗘槸 PHP 鍦 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()锛宺equire_once()锛宨nclude_once()锛実et_included_files()锛宺eadfile()锛寁irtual() 鍜 include_path銆
浠ヤ笂灏辨槸PHP鎺у埗缁撴瀯涔媔nclude鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒