Reputation: 11121
Can I somehow simplify my code below and use only one switch statement? It looks like I need 3rd switch so it would be great to use only one if possible.
$input_separator= $_REQUEST['input_separator'];
switch ($input_separator) {
case "new_line":
$input_separator="\n";
break;
case "comma":
$input_separator=",";
break;
case "none":
$input_separator="";
break;
}
$output_separator= $_REQUEST['output_separator'];
switch ($output_separator) {
case "new_line":
$output_separator="\n";
break;
case "comma":
$output_separator=",";
break;
case "none":
$output_separator="";
break;
}
Upvotes: 0
Views: 122
Reputation: 4197
Why don't you use simple function?
function convert_seperator($seperator){
$ret = '';
switch ($seperator) {
case "new_line":
$ret = "\n"; // or $ret = PHP_EOL;
break;
case "comma":
$ret = ",";
break;
case "none":
$ret = "";
break;
default:
exit('Invalid seperator');
}
return $ret;
}
$input_separator = convert_seperator($_REQUEST['input_separator']);
$output_separator = convert_seperator($_REQUEST['output_separator']);
Upvotes: 1
Reputation: 324650
Doesn't look like you need any switch
statements:
$input_separator = $_REQUEST['input_separator'] == "new_line" ? "\n" : "";
$output_separator = $_REQUEST['output_separator'] == "new_line" ? "\n" : "";
EDIT: Try this:
$separators = Array(
"new_line"=>"\n",
"comma"=>",",
"none"=>""
);
$input_separator = $separators[$_REQUEST['input_separator']];
$output_separator = $separators[$_REQUEST['output_separator']];
Upvotes: 2