1 / 14

PHP5 與 MySQL5 入門學習指南

PHP5 與 MySQL5 入門學習指南. 第 15 章 引入檔. 凱文瑞克 著. 本章大綱. 15-1 require () 15-2 include() 15-3 require 、 include 的差異 15-4 require_once() 和 include_once() 問題與討論. 15-1 require (). require() 的功能是將引入檔的內容取代 require() 所在的位置。 require() 本身並不提供回傳值 (return) 的功能

penda
Download Presentation

PHP5 與 MySQL5 入門學習指南

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP5與MySQL5入門學習指南 第 15 章 引入檔 凱文瑞克 著

  2. 本章大綱 15-1 require () 15-2 include() 15-3 require、include的差異 15-4 require_once()和include_once() 問題與討論

  3. 15-1 require () • require() 的功能是將引入檔的內容取代 require() 所在的位置。 • require() 本身並不提供回傳值 (return) 的功能 • 請特別注意, 無論執行與否 require() 的內容一定會被讀進來。 • require() 格式如下: • require (引入檔檔名) • require '引入檔檔名' • require $某個存入檔名的變數

  4. 引入檔add.inc的內容如下: <?php function add($one,$two){ return $one+$two ; } ?> 1: <html> 2: <title>Require()</title> 3: <body> 4: <?php 5: require("add.inc") ; 6: $a=10 ; 7: $b=20 ; 8: echo "Sum = ".add($a,$b) ; 9: ?> 10: </body> 11: </html> 第5行引入add.inc檔案. 第8行呼叫add.inc中的add函數

  5. 引入檔var.inc內容如下 <?php $Width=100 ; $Height=5 ; define (CrLf ,"<br>" ); ?> 1: <?php require("var.inc") ?> 2: <html> 3: <title>Require()</title> 4: <body> 5: <?php 6: echo "高度 = $Height" ; 7: echo CrLf ; 8: echo "寬度 = $Width". CrLf ; 9: echo "面積 = " ; 10: echo $Width*$Height ; 11: echo CrLf ; 12: echo "周長 = " ; 13: echo 2*($Width+$Height) ; 14: ?> 15: </body> 16: </html> var.inc定義二個變數 $Width、$Height 和一個常數 CrLf。範例 15-2第 1 行引入 var.inc。第 6-8 行顯示 $Width、$Height 變數內容。讀者可以看到 CrLf 被定義成一個文字串 "<br>", 因此在第 7 行可單獨使用或第 8 行和其他文字串接在一起使用。第 9-10 行顯示 $Width 和 $Height 乘積 (即面積)。第 11 行顯示 CrLf 常數 (即"<br>")。第 12-13 行計算並顯示周長。

  6. 15-2 include() • include() 也是將一個外部檔案內容引入到程式中使用的指令, 和 require()最大的差別在於 • include() 是在呼叫時才將引入檔的內容引入。因此在迴圈的呼叫時, 建議使用 include()。 • include() 是容許有回傳值。

  7. add_include.inc內容如下 <? echo "進入 include file <br>" ; return $a+$b ; ?> 1: <html> 2: <title>Include</title> 3: <body> 4: <?php 5: $a=10 ; 6: $b=20 ; 7: $c=include ("add_include.inc") ; 8: echo "回到主程式 : <br>" ; 9: echo "A,B二數和是 $c" ; 10: ?> 11: </body> 12: </html> 在引入檔中, 先顯示進入 include file, 然後回傳二變數的和。

  8. 1: <?php include ("add.inc") ; ?> 2: <html> 3: <title>Include</title> 4: <body> 5: <?php 6: $a=10 ; 7: $b=20 ; 8: echo "使用include的方式, Sum = " ; 9: echo add($a,$b) ; 10: ?> 11: </body> 12: </html> 引入檔add.inc的內容如下: <?php function add($one,$two){ return $one+$two ; } ?> 在本範例中說明 include() 也可以引入程式碼供程式使用。

  9. 15-3 require、include的差異 • include() 可以有回傳值 • require() 和 include()二者最大的差異是引入外部檔的錯誤處理不同。 • require() 遇到錯誤時會產生錯誤而停止執行程式, • include() 會產生警告後忽略錯誤繼續執行。

  10. 1: <html> 2: <title>引入檔載入</title> 3: <body> 4: <?php 5: echo "引入檔案前"; 6: require ("NotExist.inc") ; 7: echo "引入檔案後"; 8: ?> 9: </body> 10: </html> NotExist.inc 這個引入檔是不存在的 當 require 處理引入檔時發生錯誤, 程式立即停止執行。 所以看不到第二個提示字串。 圖中可以看到 " Failed opening required 'NotExist.inc' (include_path='')" 的錯誤訊息。換句話說, require 指令在一開始的時候就會將引入檔載入。

  11. 1: <html> 2: <title>引入檔載入</title> 3: <body> 4: <?php 5: echo "引入檔案前"; 6: include ("NotExist.inc") ; 7: echo "引入檔案後"; 8: ?> 9: </body> 10: </html> 在同一個範例中只是將指令由 require() 換成 include(), 可是由圖中可以看到第二個提示字串也顯示出來。 這表示雖然第 6 行 include() 已經產生錯誤, 但是程式依然會繼續執行。 由此可知如果你無法確認引入檔會不會產生錯誤, 又希望程式不會中斷, 那麼就必須選擇 include 函數引入外部檔案。

  12. 15-4 require_once()和include_once() • Require_once() 和 include_once() 可以防止發生重複定義函數或常數等等情形發生。 • 這二個引入檔案的函數, 所有的功能都和前面介紹的 require() 和 include() 相同。 • 被引入的檔案, 無論程式的結構如何, 引入檔只會被引入一次。

  13. 範例15-6 1: <?php 2: for ($i=1; $i<=2; $i++) 3: require_once "add.inc"; 4: echo "10+20=".add(10,20); 5: ?> 第 2-3 行是一個執行二次的 for 迴圈。 第 3 行引入 add.inc 檔案。 第 4 行藉由 add.inc 中所定義的 add() 傳回 10+20 的結果並顯示在網頁上。 將第 3 行改成 require()則會產生 Fatal error: Cannot redeclare add() (previously declared ... 由於 for 迴圈會執行二次, 那麼就表示 add.inc 要被引入二次, 這時候就會發生重複定義 add() 函數的問題。

  14. 問題與討論 • 請問引入檔的功能為何? • 請問require()與include()有何異同?請舉例說明。 • 請問require_once()與require() 有何異同?請舉例說明。 • 請問引入檔與自定函數有什麼不同。

More Related