【问题标题】:I use switch statement to include specific file but it includes the "default" also我使用 switch 语句来包含特定文件,但它也包含“默认”
【发布时间】:2013-01-04 16:56:36
【问题描述】:

这是我的代码

    <?php
    $id = isset($_GET['id']) ? $_GET['id'] : '';
    switch ($id) {
        default:include_once('default.php');
        break;
        case 'actiune':include('jocuri/actiune/index.php');
        break;
        case 'aventura':include('jocuri/aventura/index.php');
        break;
     }
     ?>
    <!--games-->
     <?php
     $game = isset($_GET['game']) ? $_GET['game'] : '';
     switch ($game) {
         case 'nz':include('jocuri/actiune/ninja-vs-zombie/index.php');
         break;
         case 'aventura':include('/jocuri/aventura/index.php');
         break;
      }
      ?>

因此,当我尝试从中访问 c​​ase 'nz' 时,它还包括顶部的默认值。那么我该怎么做才能只包含“nz”案例呢?

【问题讨论】:

    标签: php include switch-statement


    【解决方案1】:

    尝试将默认语句移动到第一个 switch 语句的底部。 switch 语句直接沿着每个 case 路径进行,因此需要 break 关键字。无论如何,默认案例将始终在案例中执行。将其放在底部将确保仅在案例失败时才会执行。

    【讨论】:

      【解决方案2】:

      据我了解,您希望在提供 game 参数时忽略“顶部”(包括 id 的文件)。 仅当未提供 game (或与您提供的案例不匹配 ['aventura', 'nz'])时,尝试添加 IF 语句以执行第一个 switch 语句/p>

      此外,您还可以这样做:

      $id = isset($_GET['id']) ? $_GET['id'] : '';
      $game = isset($_GET['game']) ? $_GET['game'] : '';
      
      $games = array('nz' => 'jocuri/actiune/ninja-vs-zombie/index.php',
                     'aventura' => '/jocuri/aventura/index.php');
      
      $ids = array('actiune' => 'jocuri/actiune/ninja-vs-zombie/index.php',
                   'aventura' => '/jocuri/aventura/index.php');
      
      if (array_key_exists($game, $games))
      {
        include($games[$game]);
      }
      else if (array_key_exists($id, $ids))
      {
        include($ids[$id]);
      }
      else include('default.php');
      

      【讨论】:

      • 我在哪里以及如何做到这一点?
      • 我已经包含了源代码以更好地解释我的想法。
      【解决方案3】:

      试试这个

          <?php
      $id = isset($_GET['id']) ? $_GET['id'] : '';
      switch ($id) {
      
          case 'actiune':include('jocuri/actiune/index.php');
          break;
          case 'aventura':include('jocuri/aventura/index.php');
          break;
         default:include_once('default.php');
          break;
       }
       ?>
      <!--games-->
       <?php
       $game = isset($_GET['game']) ? $_GET['game'] : '';
       switch ($game) {
           case 'nz':include('jocuri/actiune/ninja-vs-zombie/index.php');
           break;
           case 'aventura':include('/jocuri/aventura/index.php');
           break;
        }
        ?>
      

      【讨论】:

        【解决方案4】:

        (我确实尝试将此作为评论添加到 osulerhia 的答案中,但不能并认为如果我阅读正确,它可能会回答发帖者的问题)。

        我不确定我是否正确理解了您的问题,以及 osulerhia 的上述答案是否是您正在寻找的。

        您期望两个单独的 GET 变量(“id”和“game”),如果游戏变量是“nz”,您不想显示用于“id”的 switch 语句的默认值?

        如果这是正确的,那么您需要为您希望 switch 语句的播放方式添加某种逻辑。然后,您还需要考虑是否希望其他任何一个显示“nz”是否是“游戏”的值。你的逻辑(写得很清楚)目前是:

        Is the id variable "actiune" -> include a file
        
        Is the id variable "aventura" -> include a file
        
        Is the id variable anything else -> include the default file
        
        Is the game variable "nz" -> include a file
        
        Is the game variable "aventura" -> include a file
        

        正如您所看到的,您的两个开关是完全独立的,您需要决定要显示什么以及何时显示它,例如,如果您希望一切都像上面那样工作,但不显示来自如果游戏变量的值为“nz”,则首先切换,然后您可以将其包装在 if 语句中,例如:

        if((isset($_GET['game']) && $_GET['game'] != "nz") || (!isset($_GET['game']))) { *your original switch statement* }
        

        【讨论】:

        • 所以我在始终显示的索引页面上有一个标题和一个菜单,接下来我从菜单访问这些变量。如果我单击actiune,它包含一个actiune.php 文件,比方说,它包含另一个链接,该链接调用另一个名为“nz”的变量,并且当它被调用时,它包括switch 语句中的默认情况。我可以删除默认情况,但需要显示的是我的索引页面的内容:)
        猜你喜欢
        • 2011-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-15
        • 2011-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多