PHP涔媔nclude/require娣卞叆浜嗚В
include
- 鍏堟寜鍙傛暟缁欏嚭鐨勮矾寰勫鎵撅紝濡傛灉娌℃湁缁欏嚭鐩綍锛堝彧鏈夋枃浠跺悕锛夋椂鍒欐寜鐓 include_path 鎸囧畾鐨勭洰褰曞鎵俱
- 濡傛灉鍦 include_path 涓嬫病鎵惧埌璇ユ枃浠跺垯 include 鏈鍚庢墠鍦ㄨ皟鐢ㄨ剼鏈枃浠舵墍鍦ㄧ殑鐩綍鍜屽綋鍓嶅伐浣滅洰褰曚笅瀵绘壘銆
- 濡傛灉鏈鍚庝粛鏈壘鍒版枃浠跺垯 include 缁撴瀯浼氬彂鍑轰竴鏉¤鍛婏紱杩欎竴鐐瑰拰 require 涓嶅悓锛屽悗鑰呬細鍙戝嚭涓涓嚧鍛介敊璇
- 濡傛灉瀹氫箟浜嗚矾寰勨斺斾笉绠℃槸缁濆璺緞锛堝湪 Windows 涓嬩互鐩樼鎴栬 \ 寮澶达紝鍦 Unix/Linux 涓嬩互 / 寮澶达級杩樻槸褰撳墠鐩綍鐨勭浉瀵硅矾寰勶紙浠 . 鎴栬 .. 寮澶达級鈥斺攊nclude_path 閮戒細琚畬鍏ㄥ拷鐣ャ
require
require 鍜 include 鍑犱箮瀹屽叏涓鏍凤紝闄や簡澶勭悊澶辫触鐨勬柟寮忎笉鍚屼箣澶栥
require 鍦ㄥ嚭閿欐椂浜х敓 E_COMPILE_ERROR 绾у埆鐨勯敊璇
鎹㈠彞璇濊灏嗗鑷磋剼鏈腑姝㈣ include 鍙骇鐢熻鍛婏紙E_WARNING锛夛紝鑴氭湰浼氱户缁繍琛屻
PHP涓殑include_path
PHP鍦ㄩ亣鍒皉equire(_once)/include(_once)鐨勬寚浠ょ殑鏃跺, 棣栧厛浼氬仛濡備笅鐨勫垽鏂:
鏄惁涓虹粷瀵硅矾寰勶紵-鏄 锛 鍒欑洿鎺ュ寘鍚紝骞剁粨鏉熴-鍚︼紝 杩涘叆鍙︿竴涓昏緫(缁忚繃澶氭璋冪敤锛屽畯灞曞紑鍚庤繘鍏php_stream_fopen_with_path)瀵绘壘姝ゆ枃浠)鎺ヤ笅鏉, 鍦╛php_stream_fopen_with_path涓, 浼氬仛濡備笅鍒ゆ柇:
瑕佸寘鍚殑鏂囦欢璺緞鏄浉瀵硅矾寰勪箞(褰㈠./file,../dir/file,浠ヤ笅鐢ㄢ濈洰褰曠浉瀵硅矾寰勪唬鏇库)?-鏄,鍒欒烦杩噄nclude_path鐨勪綔鐢ㄩ昏緫,鐩存帴瑙f瀽鐩稿璺緞銆
鏇磋缁嗘渚嬭鏄庡弬闃呴笩鍝ユ枃绔狅細
http://www.laruence.com/2010/05/04/1450.html
缁撹鏈濂界殑浣跨敤缁濆璺緞
鍥犱负浣跨敤include_path鍜岀浉瀵硅矾寰勭殑鎯呭喌涓, 鎬ц兘浼氬拰瀵绘壘鐨勬鏁版湁鍏, 鏈鍧忕殑鎯呭喌涓, 濡傛灉浣犳湁10涓猧nclude_path, 閭d箞鏈澶氬彲鑳戒細閲嶈瘯11娆℃墠鑳芥壘鍒拌鍖呭惈鐨勬枃浠, 鎵浠, 鍦ㄨ兘浣跨敤缁濆璺緞鐨勬儏鍐典笅鏈濂戒娇鐢ㄧ粷瀵硅矾寰.
鍥犱负鐩綍鐩稿璺緞鐨刡asedir, 姘歌繙閮芥槸褰撳墠宸ヤ綔璺緞, 濡傛灉瑕佷娇鐢, 闇瑕佸拰瀹為檯閮ㄧ讲璺緞鐩稿叧, 鎵浠ュ疄闄呬娇鐢ㄧ殑寰堝皯(褰撶劧, 涔熸湁鍊熷姪chdir鏉ュ畬鎴愮殑妯″潡).
鍦ㄦā鍧楀寲鐨勭郴缁熻璁′腑, 涓鑸簲璇ュ湪妯″潡鍐, 閫氳繃鑾峰彇妯″潡鐨勯儴缃茶矾寰(dirname(FILE), php5.3浠ュ悗鏇存槸鎻愪緵浜咲IR甯搁噺)浠庤屼娇鐢ㄧ粷瀵硅矾寰.
include/require鍖哄埆
require()璇彞鐨勬ц兘涓巌nclude()鐩哥被浼硷紝閮芥槸鍖呮嫭骞惰繍琛屾寚瀹氭枃浠躲
鍖哄埆
include():
1.include寮曞叆鏂囦欢鐨勬椂鍊欙紝濡傛灉纰板埌閿欒锛屼細缁欏嚭鎻愮ず(Warning)锛屽苟缁х画杩愯涓嬭竟鐨勪唬鐮併2.鍦ㄦ墽琛屾枃浠舵椂姣忔閮借杩涜璇诲彇鍜岃瘎浼3.涓鑸槸鏀惧湪娴佺▼鎺у埗鐨勫鐞嗗尯娈典腑4.PHP鑴氭湰鏂囦欢鍦ㄨ鍒癷nclude()璇彞鏃讹紝鎵嶅皢瀹冨寘鍚殑鏂囦欢璇诲彇杩涙潵5.include()鏄湁鏉′欢鍖呭惈鍑芥暟require():
1.require寮曞叆鏂囦欢鐨勬椂鍊欙紝濡傛灉纰板埌閿欒锛屼細缁欏嚭鎻愮ず(Fatal error)锛屽苟鍋滄杩愯涓嬭竟鐨勪唬鐮併2.鏂囦欢鍙鐞嗕竴娆★紙瀹為檯涓婏紝鏂囦欢鍐呭鏇挎崲require()璇彞锛3.閫氬父鏀惧湪PHP鑴氭湰绋嬪簭鐨勬渶鍓嶉潰4.PHP绋嬪簭鍦ㄦ墽琛屽墠锛屽氨浼氬厛璇诲叆require()璇彞鎵寮曞叆鐨勬枃浠讹紝浣垮畠鍙樻垚PHP鑴氭湰鏂囦欢鐨勪竴閮ㄥ垎銆5.require()鍒欐槸鏃犳潯浠跺寘鍚嚱鏁//鍙橀噺$ok鏃犺鏄綍鍊硷紝1.php閮戒細琚寘鍚繘鏉鍦≒HP绋嬪簭鎵ц鍓嶏紝灏辫鍏equire()璇彞]if($ok){require'1.php';}//鍙橀噺$ok涓虹湡锛屽垯鍖呭惈鏂囦欢2.phpif($ok){ include'2.php';}缁撹
- incluce鍦ㄧ敤鍒版椂鍔犺浇
- require鍦ㄤ竴寮濮嬪氨鍔犺浇
- _once鍚庣紑琛ㄧず宸插姞杞界殑涓嶅姞杞
- 濡傛灉鍙兘鎵ц澶氭鐨勪唬鐮侊紝鍒欎娇鐢╮equire()鏁堢巼姣旇緝楂
- 濡傛灉姣忔鎵ц浠g爜鏃舵槸璇诲彇涓嶅悓鐨勬枃浠讹紝鎴栬呮湁閫氳繃涓缁勬枃浠惰凯浠g殑寰幆锛屽氨浣跨敤include()璇彞
浠ヤ笂灏辨槸PHP涔媔nclude/require娣卞叆浜嗚В鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒