【问题标题】:I want a PHP include to be interrupted我希望 PHP 包含被中断
【发布时间】:2010-11-10 00:42:23
【问题描述】:

这样的东西在一个名为“header.php”的文件中,我网站上的所有文件都包括:

switch (str_replace('/folder_name/','',$_SERVER['REQUEST_URI'])){
    case 'index.php':
    $page_title='main page';
    break;
    case 'page1.php':
    $page_title='page 1 is here';
    break;
    case 'page2.php':
    $page_title='this is page 2';
    break;
}

然后是一些所有页面共有的 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
etc... 

我能否做到这一点,对于 switch 中的一种情况,包含停止在那里?我在问是否有办法从包含中中断包含。例如,它可能看起来像:

case 'page3.php':
$page_title='the title for page 3';
STOP_THE_INCLUDE
break;

这可能吗?

【问题讨论】:

    标签: php include switch-statement


    【解决方案1】:

    是的,您可以简单地在包含中使用return;

    【讨论】:

    • 虽然我同意您的解决方案,但我不得不说,从内部中断包含听起来像 可怕 应用程序架构。 (同样,这与您的回答无关,+1)
    • 是的,我什至不认为我会保持这种设置,我只是好奇如果我愿意我会怎么做。
    【解决方案2】:

    当您退出开关时,使用 IF 来避免其余的包含?像这样:

     $stopInclude = false;
     switch (str_replace('/folder_name/','',$_SERVER['REQUEST_URI'])){ 
       case 'index.php': 
         $page_title='main page'; 
         break; 
       case 'page1.php': 
         $page_title='page 1 is here'; 
         break; 
       case 'page2.php': 
         $page_title='this is page 2'; 
         break;
       case 'page3.php': 
         $page_title='the title for page 3'; 
         $stopInclude = true;
         break; 
     }
     if (!$stopInclude) {
       //whole rest of the include
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2014-05-20
      • 2012-06-01
      相关资源
      最近更新 更多