【问题标题】:PHP - Add variable to switch statement valuePHP - 添加变量以切换语句值
【发布时间】:2015-11-19 01:30:03
【问题描述】:

我有以下 switch 语句:

$html = '<div class="'. $some_value . '">';

switch ($some_value) {
    case "one":
        return $html . 'One Biscuit</div>';
        break;
    case "two":
        return $html . 'Two Chimps</div>';
        break;
    case "three":
        return $html . 'Three Pies</div>';
        break;
    default:
        return $html . 'Meh...</div>';
}

注意到我是如何将$html 变量添加到每个案例的吗?不好...是否可以将其仅添加一次到 switch 语句的最终值?我正在尝试将最终值包装在动态 HTML 中。

【问题讨论】:

  • 一种更复杂的方法是使用输出缓冲区,ob_start(),打印出所有内容,然后 $var = ob_get_clean();虽然它可能比你需要的更复杂
  • 而且当你在switch语句中使用return时,你真的不需要break!
  • 注意:您可以将案例放在一个数组中进行查找。

标签: php switch-statement


【解决方案1】:

这个呢:

switch($some_value){
    case 'one':
        $var="One Biscuit";
    break;
    case 'two':
        $var="Two Chimps";
    break;
    case 'three':
        $var="Three Pies";
    break;
    default:
        $var="Meh...";
    break;
}
$html="<div class=".$some_value.">".$var."</div>";

【讨论】:

  • 投票,因为 HTML 是在结束
    标签的末尾生成的。
【解决方案2】:

将字符串存储在新变量中。此外,您不需要在 返回语句之后休息。

$html = '<div class="'. $some_value . '">';

$str = null;
switch ($some_value) {
    case "one":
        $str = 'One Biscuit</div>';
        break;
    case "two":
        $str = 'Two Chimps</div>';
        break;
    case "three":
        $str = 'Three Pies</div>';
        break;
    default:
        $str = 'Meh...</div>';
        break;
}
return $html.$str;

【讨论】:

    【解决方案3】:

    一种方法:

    $html = '<div class="'. $some_value . '">';
    $v = 'Meh...</div>';
    switch ($some_value) {
        case "one":
            $v = 'One Biscuit</div>';
            break;
        case "two":
            $v = 'Two Chimps</div>';
            break;
        case "three":
            $v = 'Three Pies</div>';
            break;
    }
    
    $html .= $v;
    

    由于您使用的是return,因此您最终只需返回一次: return $html.$v

    此外,您可以将参数定义为默认值,如下所示:

    function someFunction(DUNNO_YOUR_PARAMS, $v = 'Meh...'){
        $v .= '</div'>;
        // rest of code
    

    【讨论】:

      【解决方案4】:

      其他方式是将数据保存在数组中:

      $some_value = 'two';
      //
      $data = array(//this data could be stored in a database table
        'one'  => 'One Biscuit',
        'two'  => 'Two Chimps',
        'three'=> 'Three Pies',
        'default' => 'Meh...'
      );
      
      $html = '<div class="'.$some_value.'">'.(isset($data[$some_value])?$data[$some_value]:$data['default']).'</div>';
      var_dump($html);
      

      结果:

      string '<div class="two">Two Chimps</div>' (length=33)
      

      在某些情况下,array 比 switch 更快:In PHP what's faster, big Switch statement, or Array key lookup

      【讨论】:

      • 我同意这个答案,但是提到这样的事情的性能会适得其反,完全没有抓住重点。
      • 我同意@PeeHaa
      【解决方案5】:

      如果您的 html 较长,可能更易于维护的另一种解决方案:

      <?php
      ob_start();
      echo '<div class="'.$some_var.'">';
      /*
      Be sure the file exists in the location you want
      You can change items to the name of any directory you want just be
      sure the file exists in the end. In the case of linux be sure the file
      is the exact same name...
      */
      include('items/'.$some_var.'.php');
      echo '</div>';
      $output = ob_get_clean();
      return $output;
      ?>
      

      在您的文件中,您只需放入所需的 html 代码即可。
      示例三.php(它实际上只是 html 或纯文本,除非您处理某些内容:

      Three pies
      

      如果您有太多这么简单的文件,这可能会被重构。但如果它更复杂,您可以包含更多内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-24
        • 2021-02-04
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多