can this be made into an IF ternary to shorten the code? I don't know how to do it with multiple elseif clause
if ($ext == "m") {
$extype = "mp3";
}elseif ($ext == "w") {
$extype = "wav";
}elseif ($ext == "a"){
$extype = "aac";
}
the is the full function
function loc($id,$type,$ext) //loc($id,'n',$ext)
{
if (($query = mysql_query("SELECT loc FROM names WHERE id = '".(int)$id."'")) !== false)
{
if (mysql_num_rows($query) > 0)
if ($ext == "m") {
$extype = ".mp3";
}elseif ($ext == "w") {
$extype = "开发者_如何学C.wav";
}elseif ($ext == "a") {
$extype = ".aac";
}
return '/rec/'.mysql_result($query,0,'loc').$id.'-'.($type=='n'?'n':'s').$extype);
}
return 'Error Not Found';
}
You can do it by nesting ternary operators.
Don't do that. It will make your code unreadable and harder to maintain.
There are far better solutions to whatever you're trying to do. For the love of everything that is good in the world, don't you dare write code like this:
($var1 = "a") ? (a) : ((($var2 = "b") ? (b) : ((($var3 = "c") ? (c) : ((($var4 = "d") ? (d) : ((($var5 = "e") ? (e) : ((($var6 = "f") ? (f) : ((($var7 = "g") ? (g) : ((h))))))))))))));
The person who has to maintain this will become a psycopath.
Use a switch, use an array with keys, do anything but this.
Switch:
function loc($id,$type,$ext) //loc($id,'n',$ext)
{
if (($query = mysql_query("SELECT loc FROM names WHERE id = '".(int)$id."'")) !== false)
{
if (mysql_num_rows($query) > 0)
switch ($ext) {
case "m" :
$exttype = ".mp3"; //Make sure you terminate your statements with the ; mark.
break;
case "w" :
$exttype = ".wma";
break;
case "a" :
$exttype = ".acc";
break;
default : //All cases that didn't match.
$exttype = "";
break;
}
return '/rec/'.mysql_result($query,0,'loc').$id.'-'.($type=='n'?'n':'s').$extype);
}
return 'Error Not Found';
}
The switch will match for all cases specified and has a default action ones that it didn't match (example: you have a "O" in that variable, then the string becomes a blank value)
Arrays
function loc($id,$type,$ext) //loc($id,'n',$ext)
{
$list["m"] = ".mp3";
$list["w"] = ".wma";
$list["a"] = ".acc";
if (($query = mysql_query("SELECT loc FROM names WHERE id = '".(int)$id."'")) !== false)
{
if (mysql_num_rows($query) > 0)
$extype = $list[$ext];
return '/rec/'.mysql_result($query,0,'loc').$id.'-'.($type=='n'?'n':'s').$extype);
}
return 'Error Not Found';
}
This array method consumes a bit more memory because the array gets stored, but by specifying the key to the array and matching it with it's value you get that value out of it. The down side being you need to program around a default for items that are NOT in the array (again, if you have the variable with "O", it's not going to find a matching array key-value pair and come up as an error).
Looks like you'd be best off with a switch rather than IF/ELSE's
switch ($ext) {
case 'm':
$extype = '.mp3';
break;
case 'w':
$extype = '.wav';
break;
case 'a':
$extype = '.aac';
break;
default:
$extype = '.wma';
break;
}
Also, it sounds like you need to read the documentation on arrays and control structures at php.net.
There's this thing called a "switch" statement.
Don't be afraid of it - it's your friend.
Yes, it's possible, but I wouldn't recommend it:
echo (($ext == "m") ? $extype = "mp3" : (($ext == "w") ? $extype = "wav" : (($ext == "a") ? $extype = "aac" : '')));
精彩评论