本文章詳細的介紹了關於在php中的文件調用測試包括了include(), require() ,include_once(),require_once()等多種交換調用,有需要的朋友可以參考一下。
7.3.1 Include的使用,可以包含相同的文件多次
<?php
include 'demo1.php';
include 'demo1.php';
include 'demo1.php';
?>
輸出結果如
代碼如下 復制代碼e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.2 include_once使用上和include沒什麼區別,但是調用多次只會包含相同的文件一次
代碼如下 復制代碼
<?php
include_once 'demo1.php';
include_once 'demo1.php';
include_once 'demo1.php';
?>
結果如下
代碼如下 復制代碼 e10adc3949ba59abbe56e057f20f883e
7.3.3 require() 語句包含並運行指定文件。
代碼如下 復制代碼<?php
require 'demo1.php';
require 'demo1.php';
require 'demo1.php';
?>
結果如下
代碼如下 復制代碼e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.4 require_once() 語句在腳本執行期間包含並運行指定文件.但是不重復包含相同的文件。
代碼如下 復制代碼<?php
require_once 'demo1.php';
require_once 'demo1.php';
require_once 'demo1.php';
?>
輸出結果如下
代碼如下 復制代碼 e10adc3949ba59abbe56e057f20f883e
7.3.5 include與require的區別
Include後面如果還有其他代碼,當調用include出錯時,後面的代碼還會繼續執行,但是require則不會。
Include在調用一個不存在的文件時,會給出警告,但是會繼續執行後面的代碼。
代碼如下 復制代碼<?php
include 'demo111.php';
echo('this is demo13.php');
?>
輸出結果如下
代碼如下 復制代碼
Warning: include(demo111.php) [function.include]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo13.php on line 2
Warning: include() [function.include]: Failed opening 'demo111.php' for inclusion (include_path='.;C:php5pear') in D:AppServwwwBasic7demo13.php on line 2
this is demo13.php
Require在調用一個不存在的文件時,會給出一個錯誤,並中止代碼的執行。
代碼如下 復制代碼<?php
require 'demo111.php';
echo('this is demo14.php');
?>
輸出結果如下
代碼如下 復制代碼
Warning: require(demo111.php) [function.require]: failed to open stream: No such file or directory in D:AppServwwwBasic7demo14.php on line 2
Fatal error: require() [function.require]: Failed opening required 'demo111.php' (include_path='.;C:php5pear') in D:AppServwwwBasic7demo14.php on line 2