I have a string containing a file extension. If this extension = wmv, mov, swf, avi . . ., I need to set a variable = Video.
I can do this with switch (see code) but I’m sure there is a better way to do it .
any help will be appreciated.
switch ($tmp_ext){
case ($tmp_ext=="wmv"):
$pieces[0]="video";
break;
case ($tmp_ext=="mpg"):
$pieces[0]="video";
break;
case ($tmp_ext=="swf"):
$pieces[0]="video";
break;
case ($tmp_ext=="avi"):
$pieces[0]="video";
break;
case ($tmp_ext=="flw"):
$pieces[0]="video";
break;
case ($tmp_ext=="swf"):
$pieces[0]="video";
break;
}
The format of the switch is also wrong. Currently the format you are using is really switch(true) as you are using conditionals in the cases( case ( $tmp_ext==“wmv”): ). http://php.net/manual/en/control-structures.switch.php
Fall-through is another option
<?php
$tmp_ext = 'swf';
switch ($tmp_ext){
case "wmv":
case "mpg":
case "swf":
case "avi":
case "flw": //flv?
case "swf":
$pieces[0]="video";
break;
case "jpg":
$pieces[0]="image";
break;
}
var_dump($pieces);
?>
Though in_array is less space consuming and I would use that…