2009-11-23 35 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),所以这可以被看作是“最佳实践”。
相关问题