久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      PHP 中 include 和 require 的區(qū)別詳解


      1、概要

      require() 語(yǔ)句的性能與 include() 相類似,都是包括并運(yùn)行指定文件。不同之處在于:對(duì) include() 語(yǔ)句來(lái)說,在執(zhí)行文件時(shí)每次都要進(jìn)行讀取和評(píng)估;而對(duì)于 require() 來(lái)說,文件只處理一次(實(shí)際上,文件內(nèi)容替換 require() 語(yǔ)句)。這就意味著如果可能執(zhí)行多次的代碼,則使用 require() 效率比較高。另外一方面,如果每次執(zhí)行代碼時(shí)是讀取不同的文件,或者有通過一組文件迭代的循環(huán),就使用 include() 語(yǔ)句。

      require() 的使用方法如:

      require("myfile.php")

      這個(gè)語(yǔ)句通常放在 PHP 腳本程序的最前面。PHP 程序在執(zhí)行前,就會(huì)先讀入 require() 語(yǔ)句所引入的文件,使它變成 PHP 腳本文件的一部分。

      include() 使用方法和 require 一樣如:

      include("myfile.php")

      這個(gè)語(yǔ)句一般是放在流程控制的處理區(qū)段中。

      PHP 腳本文件在讀到 include() 語(yǔ)句時(shí),才將它包含的文件讀取進(jìn)來(lái)。這種方式,可以把程式執(zhí)行時(shí)的流程簡(jiǎn)單化。

      • incluce 在用到時(shí)加載
      • require 在一開始就加載
      • _once 后綴表示已加載的不加載

      PHP 系統(tǒng)在加載PHP程序時(shí)有一個(gè)偽編譯過程,可使程序運(yùn)行速度加快。但 incluce 的文檔仍為解釋執(zhí)行。include 的文件中出錯(cuò)了,主程序繼續(xù)往下執(zhí)行,require 的文件出錯(cuò)了,主程序也停了,所以包含的文件出錯(cuò)對(duì)系統(tǒng)影響不大的話(如界面文件)就用 include,否則用 require。

      require() 和 include() 語(yǔ)句是語(yǔ)言結(jié)構(gòu),不是真正的函數(shù),可以像 php 中其他的語(yǔ)言結(jié)構(gòu)一樣,例如 echo() 可以使用 echo(“ab”) 形式,也可以使用 echo “abc” 形式輸出字符串 abc。require() 和i nclude() 語(yǔ)句也可以不加圓括號(hào)而直接加參數(shù)。

      include_once() 和 require_once() 語(yǔ)句也是在腳本執(zhí)行期間包括運(yùn)行指定文件。此行為和 include() 語(yǔ)句及 require() 類似,使用方法也一樣。唯一區(qū)別是如果該文件中的代碼已經(jīng)被包括了,則不會(huì)再次包括。這兩個(gè)語(yǔ)句應(yīng)該用于在腳本執(zhí)行期間,同一個(gè)文件有可能被包括超過一次的情況下,確保它只被包括一次,以避免函數(shù)重定義以及變量重新賦值等問題。


      2、詳解

      2.1 報(bào)錯(cuò)

      include 引入文件的時(shí)候,如果碰到錯(cuò)誤,會(huì)給出提示,并繼續(xù)運(yùn)行下邊的代碼。

      require 引入文件的時(shí)候,如果碰到錯(cuò)誤,會(huì)給出提示,并停止運(yùn)行下邊的代碼。

      用例子來(lái)說話,寫兩個(gè) php 文件,名字為 test-include.php 和 test-require.php,注意相同的目錄中,不要存在一個(gè)名字是 test-nothing.php 的文件。

      test-include.php

      <?php include test-nothing.php; echo abc; ?>

      test-require.php

      <?php require test-nothing.php; echo abc; ?>

      瀏覽 http://localhost/test-include.php,因?yàn)闆]有找到 test-nothing.php 文件,我們看到了報(bào)錯(cuò)信息,同時(shí),報(bào)錯(cuò)信息的下邊顯示了 abc,你看到的可能是類似下邊的情況:

      Warning: include(test-nothing.php) [function.include]: failed to open stream: No such file or directory in D:wwwtest-include.php on line 2    Warning: include() [function.include]: Failed opening 'test-nothing.php' for inclusion (include_path='.;C:php5pear') in D:wwwtest-include.php on line 2    abc

      瀏覽 http://localhost/test-require.php,因?yàn)闆]有找到 test-nothing.php 文件,我們看到了報(bào)錯(cuò)信息,但是,報(bào)錯(cuò)信息的下邊沒 有顯示abc,你看到的可能是類似下邊的情況:

      Warning: require(test-nothing.php) [function.require]: failed to open stream: No such file or directory in D:wwwtest-require.php on line 2    Fatal error: require() [function.require]: Failed opening required 'test-nothing' (include_path='.;C:php5pear') in D:wwwtest-require.php on line 2 

      2.2 文件引用方式

      include() 執(zhí)行時(shí)需要引用的文件每次都要進(jìn)行讀取和評(píng)估,require() 執(zhí)行時(shí)需要引用的文件只處理一次(實(shí)際上執(zhí)行時(shí)需要引用的文件內(nèi)容替換了 require() 語(yǔ)句)可以看出若有包含這些指令之一的代碼和可能執(zhí)行多次的代碼,則使用 require() 效率比較高,若每次執(zhí)行代碼時(shí)相讀取不同的文件或者有通過一組文件疊代的循環(huán),就使用 include(),可以給想要包括的文件名設(shè)置變量,當(dāng)參數(shù)為 include() 時(shí)使用這個(gè)變量。

      原文鏈接:https://blog.csdn.net/shenpengchao/article/details/52326233

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)