{php 控制流程}

來源:互聯網
上載者:User
Table of Contents 簡介 if else elseif/else if switch
while do-while for foreach

break continue goto declare return require include require_once include_once

###簡單說說:PHP檔案包含語句有 include、include_once、require、require_once區別

              

1、include()格式1:include(path/filename);格式2:include 'filename';一、include()語句將在其被調用的位置處包含一個檔案。包含一個檔案與在該語句所在位置複製制定檔案的資料具有相同內

容的效果。使用include()時可以忽略括弧。二、可以根據條件來執行include()語句。在條件陳述式中使用include()有個怪現象,它必須包圍在語句塊大括弧中,或者用

其他語句包圍符括起來。

2、include_once() 格式1:include_once(filename);格式2:include_once 'filename';include_once()函數的作用與include相同,不過它會首先驗證是否已經包含了該檔案。如果已經包含,則不再執行

include_once。否則,則必須包含該檔案。除了這一點與include完全相同。3、require() 格式1:require(filename);格式2:require 'filename';require()在很大程度上與include相同,都是將一個模板檔案包含到require調用坐在的位置。require和include之間有兩點重要的區別:

首先,無論require的位置如何,制定檔案都將包含到出現require的指令碼中。例如,即使require放在計算結果為假的if語

   句中,依然會包含指定檔案。

第二個重要的區別是:require出錯時,指令碼將停止運行,而在使用include的情況下,指令碼將繼續執行。4、require_once() 格式1:require_once(filename);格式2:require_once 'filename';隨著網站越來越大,可能會出現重複包含某些檔案。這也許不是問題,但又是修改了所包含檔案的變數後,卻由於後面再次包

含原來的檔案而被覆蓋,可能不希望出現這種情況。還可能出現另一個問題,即所包含檔案中函數名的衝突。使用

require_once就可以解決這些問題。require_once函數確保檔案只包含一次。在遇到require_once後,後面再試圖包含相同的檔案時將被忽略。include()在引入檔案時候出錯時,指令碼會繼續向下執行require() 在引入檔案時候出錯時,指令碼不會繼續向下執行


###流程式控制制的替代文法

PHP 提供了一些流程式控制制的替代文法,包括 if,while,for,foreach 和 switch。

替代文法的基本形式是把左花括弧({)換成冒號(:),把右花括弧(})分別換成endif;,endwhile;,endfor;,endforeach; 以及 endswitch;。

              

$b) { echo " 123 " ;} elseif ($a == $b) { echo " 456 " ;} else { echo " 789 " ;} ?>

              

~

~

  • 聯繫我們

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