PHP switch语句显示文件类型含义

时间:2017-08-07 19:46:54

标签: php

输入 $ filetype 是文件扩展名,所以如果我们说它的PHP $ def应该等于" Web"。 但输出只是" Image"无论输入中的文件类型是什么。

$filetype = "PHP";
switch(strtolower($filetype)) {
    case ("jpeg" || "jpg" || "png" || "ico" || "gif" || "svg" || "ps" || "psd" || "tif" || "tiff" || "ai" || "bmp"):
        $def = 'Image';
        break;
    case ("aif" || "cda" || "mid" || "midi" || "mp3" || "mpa" || "ogg" || "wav" || "wma" || "wpl"):
        $def = 'Audio';
        break;
    case ("7z" || "arj" || "deb" || "pkg" || "rar" || "gz" || "tar.gz" || "z" || "zip"):
        $def = 'Archive';
        break;
    case ("bin" || "dmg" || "iso" || "toast" || "vcd"):
        $def = 'Disc';
        break;
    case ("csv" || "dat" || "db" || "dbf" || "log" || "mdb" || "sav" || "sql" || "tar" || "xml"):
        $def = 'Data';
        break;
    case ("apk" || "bat" || "cgi" || "pl" || "com" || "exe" || "gadget" || "jar" || "py" || "wsf"):
        $def = 'Executable';
        break;
    case ("fnt" || "fon" || "otf" || "ttf"):
        $def = 'Font';
        break;
    case ("asp" || "aspx" || "cer" || "cfm" || "css" || "htm" || "html" || "js" || "jsp" || "part" || "php" || "rss" || "xhtml"):
        $def = 'Web';
        break;
    case ("key" || "odp" || "pps" || "ppt" || "pptx"):
        $def = 'Presentation';
        break;
    case ("c" || "class" || "cpp" || "cs" || "h" || "java" || "sh" || "swift" || "vb"):
        $def = 'Programming';
        break;
    case ("ods" || "xlr" || "xls" || "xlsx"):
        $def = 'Spreadsheet';
        break;
    case ("3g2" || "3gp" || "avi" || "flv" || "h264" || "m4v" || "mkv" || "mov" || "mp4" || "mpg" || "mpeg" || "rm" || "swf" || "vob" || "wmv"):
        $def = 'Video';
        break;
    case ("doc" || "docx" || "odt" || "pdf" || "rtf" || "tex" || "txt" || "wks" || "wps" || "wpd"):
        $def = 'Document';
        break;
    default:
        $def = '';
}
echo $def;

如何从交换机获得正确的$ def输出?

1 个答案:

答案 0 :(得分:2)

你的开关盒情况似乎是错误的。使用它如下所示

$filetype = "PHP";

 switch(strtolower($filetype)) {
case 'jpeg';
    case 'jpg';
    case 'png';
         $def = 'image';
    break;
     case 'asp';
    case 'php';
         $def = 'web';
     break;
     default:
      $def = '';
      break;

 }
echo $def; // web