【问题标题】:how to properly use switch function in php如何在php中正确使用switch函数
【发布时间】:2014-03-11 23:33:35
【问题描述】:

我想在单选按钮的值上使用开关功能,但我不知道如何正确使用它。

 if(isset($_POST['Itemtype']))
    {
    $Itemtype = $_POST["Itemtype"];                             
switch ($Itemtype)
    {
    case "Ingredient":
    $Brandname = $_POST["Brandname"];

if(isset($_POST['Brandname']))
    {
    $try2 = "Brandname working";
    }
    else
    {
    $errormsg = 'error on branchname';
    }
    break;
    case "Miscellaneous":
    $Size = $_POST["Size"];
    $Color = $_POST["Color"];
    if(isset($_POST['Size']))
    {
    $try2 = "Misc working";
    }
    else
    {
    $errormsg = 'error on size';
    break;
    }
  else
{
$errormsg = 'error5';
}

我的字符串部分不好 :) 但它不检查品牌名称是否设置或为空。

【问题讨论】:

  • case 'Ingredient' 因为它是一个字符串
  • 我应该让case "Ingredient";吗?
  • case 'Ingredient':case 'Miscellaneous': 你只需要''

标签: php switch-statement


【解决方案1】:

问题是您的字符串不在引号内 ex : case Miscellaneous: 应该是 case "Miscellaneous":

【讨论】:

    【解决方案2】:

    您忘记将成分包装在 " 中,因此您的代码应如下所示:

    if(isset($_POST['Itemtype'])){
    
      $Itemtype = $_POST["Itemtype"];  
      switch ($Itemtype){
    
        case "Ingredient":
           $Brandname = $_POST["Brandname"];
    
           if(isset($_POST['Brandname'])){
            $try2 = "Brandname working";
           }
           else{
            $errormsg = 'error on branchname';
           }
        break;
    
    
        case "Miscellaneous":
          $Size = $_POST["Size"];
          $Color = $_POST["Color"];
    
          if(isset($_POST['Size'])){
            $try2 = "Misc working";
          }
          else{
            $errormsg = 'error on size';
          }
        break;
    
      }
    
    }
    else{
      $errormsg = 'error5';
    }
    

    【讨论】:

    • 是的,我的错...当那个只读的东西出现时,我正要编辑它:)我弄清楚了一切...
    猜你喜欢
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多