php裡的include和require

來源:互聯網
上載者:User
今天偶然看到的,摘錄之
在PHP變成中,include()與require()的功能相同,但在用法上卻有一些不同,include()是有條件包含函數,而require()則是無條件包含函數。例如在下面的一個例子中,如果變數$somgthing為真,則將包含檔案somefile:

if($something){
 include("somefile");
}

  但不管$something取何值,下面的代碼將把檔案somefile包含進檔案裡:

if($something){
 require("somefile");
}

  下面的這個有趣的例子充分說明了這兩個函數之間的不同。

$i = 1;
while ($i < 3) {
 require("somefile.$i");
 $i++;
}

  在這段代碼中,每一次迴圈的時候,程式都將把同一個檔案包含進去。很顯然這不是程式員的初衷,從代碼中我們可以看出這段代碼希望在每次迴圈時,將不同的檔案包含進來。如果要完成這個功能,必須求助函數include():

$i = 1;
while ($i < 3) {
 include("somefile.$i");
 $i++;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.