PHP中auto_prepend_file與auto_append_file用法執行個體分析,autoprependfile_PHP教程

來源:互聯網
上載者:User

PHP中auto_prepend_file與auto_append_file用法執行個體分析,autoprependfile


本文執行個體講述了PHP中auto_prepend_file與auto_append_file的用法,是PHP程式設計中比較實用的技巧。分享給大家供大家參考。具體方法如下:

如果需要將檔案require到所有頁面的頂部與底部。

第一種方法:在所有頁面的頂部與底部都加入require語句。
例如:

require('header.php');//頁面本文內容部分require('footer.php');

但這種方法如果需要修改頂部或底部require的檔案路徑,則需要修改所有分頁檔。而且需要每個頁面都加入require語句,比較麻煩。

第二種方法:使用auto_prepend_file與auto_append_file在所有頁面的頂部與底部require檔案。

php.ini中有兩項:

auto_prepend_file 在頁面頂部負載檔案
auto_append_file 在頁面底部負載檔案

使用這種方法可以不需要改動任何頁面,當需要修改頂部或底部require檔案時,只需要修改auto_prepend_file與auto_append_file的值即可。

例如:修改php.ini,修改auto_prepend_file與auto_append_file的值。

auto_prepend_file = "/home/fdipzone/header.php"auto_append_file = "/home/fdipzone/footer.php"

修改後重啟伺服器,這樣所有頁面的頂部與底部都會require /home/fdipzone/header.php 與 /home/fdipzone/footer.php

注意:auto_prepend_file 與 auto_append_file 只能require一個php檔案,但這個php檔案內可以require多個其他的php檔案。

如果不需要所有頁面都在頂部或底部require檔案,可以指定某一個檔案夾內的分頁檔才調用auto_prepend_file與auto_append_file
在需要頂部或底部負載檔案的檔案夾中加入.htaccess檔案,內容如下:

php_value auto_prepend_file "/home/fdipzone/header.php"php_value auto_append_file "/home/fdipzone/footer.php"

這樣在指定.htaccess的檔案夾內的分頁檔才會載入 /home/fdipzone/header.php 與 /home/fdipzone/footer.php,其他分頁檔不受影響。

使用.htaccess設定,比較靈活,不需要重啟伺服器,也不需要管理員權限,唯一缺點是目錄中每個被讀取和被解釋的檔案每次都要進行處理,而不是在啟動時處理一次,所以效能會有所降低。

希望本文所述對大家PHP程式設計的學習有所協助。


php中的return的用法,作用與例子

return() 也會終止 eval() 語句或者指令檔的執行。如果在全域範圍中調用,則當前指令檔中止運行。如果當前指令檔是被 include() 的或者 require() 的,則控制交回調用檔案。此外,如果當前指令碼是被 include() 的,則 return() 的值會被當作 include() 調用的傳回值。如果在主指令檔中調用 return(),則指令碼中止運行。如果當前指令檔是在 php.ini 中的配置選項 auto_prepend_file 或者 auto_append_file 所指定的,則此指令檔中止運行。b.php  

php中的return的用法,作用與例子

如果在一個函數中調用 return() 語句,將立即結束此函數的執行並將它的參數作為函數的值返回。return() 也會終止 eval() 語句或者指令檔的執行。

如果在全域範圍中調用,則當前指令檔中止運行。如果當前指令檔是被 include() 的或者 require() 的,則控制交回調用檔案。此外,如果當前指令碼是被 include() 的,則 return() 的值會被當作 include() 調用的傳回值。如果在主指令檔中調用 return(),則指令碼中止運行。如果當前指令檔是在 php.ini 中的配置選項 auto_prepend_file 或者 auto_append_file 所指定的,則此指令檔中止運行。

例:
function min($a, $b){
return $a < $b ? $a : $b;//這裡是做為函數傳回值,下面的語句不再執行了
$a++;
}
?>

a.php
include("b.php");
echo "a";
?>
b.php
echo "b";
return;
echo "c";//這裡將不被執行
?>
上面的結果將輸出ba
 

http://www.bkjia.com/PHPjc/882912.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/882912.htmlTechArticlePHP中auto_prepend_file與auto_append_file用法執行個體分析,autoprependfile 本文執行個體講述了PHP中auto_prepend_file與auto_append_file的用法,是PHP程式設計中比較實...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.