2是否在交换机函数中返回有效代码?

时间:2014-12-15 13:25:32

标签: php switch-statement

function getFileExtension($fileType)
{
    switch($fileType)
    {
        case "image/png":
            return "png";
            return true;
            break;

        case "image/png":
            return "x-png";
            return true;
            break;

        case "mage/pjpeg":
            return "jpg";
            return true;
            break;

        case "image/jpeg":
            return "jpg";
            return true;
            break;

         default:
            return false;
    }
}

1 个答案:

答案 0 :(得分:2)

永远不会执行第二个返回语句。如果要返回2个值,则返回包含2个值的数组..

function getFileExtension($fileType)
{
  $results = array();
  switch($fileType)
  {
    case "image/png":
        $results['type'] = "png";
        $results['status'] = true;
        break;

    case "image/png":
        $results['type'] = "x-png";
        $results['status'] = true;
        break;

    case "mage/pjpeg":
        $results['type'] = "jpg";
        $results['status'] = true;
        break;

    case "image/jpeg":
        $results['type'] = "jpg";
        $results['status'] = true;
        break;

     default:
        $results['type'] = "";
        $results['status'] = false;
  }
  return $results;
}