2009-11-23 33 views
6

這是index.php中的代碼,只有<?php,但沒有?>,這是我第一次看到類似這樣的代碼,有什麼理由?爲什麼php標籤在drupal中沒有關閉?

<?php 
// $Id: index.php,v 1.94 2007/12/26 08:46:48 dries Exp $ 

/** 
* @file 
* The PHP page that serves all page requests on a Drupal installation. 
* 
* The routines here dispatch control to the appropriate handler, which then 
* prints the appropriate page. 
* 
* All Drupal code is released under the GNU General Public License. 
* See COPYRIGHT.txt and LICENSE.txt. 
*/ 

require_once './includes/bootstrap.inc'; 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 

$return = menu_execute_active_handler(); 

// Menu status constants are integers; page content is a string. 
if (is_int($return)) { 
    switch ($return) { 
    case MENU_NOT_FOUND: 
     drupal_not_found(); 
     break; 
    case MENU_ACCESS_DENIED: 
     drupal_access_denied(); 
     break; 
    case MENU_SITE_OFFLINE: 
     drupal_site_offline(); 
     break; 
    } 
} 
elseif (isset($return)) { 
    // Print any value (including an empty string) except NULL or undefined: 
    print theme('page', $return); 
} 

drupal_page_footer(); 

回答

12

省略結束標記可以防止意外將尾隨白色空間注入到響應中。

是一些框架中的常見編碼習慣,如Zend

+6

對於那些不知道的人來說,這可能是值得說明的,你不希望意外的空白空間的原因是它是一個非常快捷的方式來結束'headers already sent'問題。 – 2009-11-23 09:15:33

+0

它也被任何遵循[PSR-2](http://www.php-fig.org/psr/psr-2/)編碼標準的框架/庫使用。 – 2014-05-12 19:16:03

10

省略PHP結束標記是Drupal Coding Standards的一部分。

自從Drupal 4.7以來,代碼文件末尾的?>被故意省略。這包括模塊和包含文件。

  • 刪除它消除了在文件的結尾不需要的空格的可能性這可能會導致「頭部已經發送」的錯誤,XHTML/XML驗證問題和其他問題:造成這種情況的原因,可以作爲概括。
  • The closing delimiter at the end of a file is optional
  • PHP.net本身從文件末尾刪除結尾分隔符(例如:prepend.inc),所以這可以被看作是「最佳實踐」。
相關問題