another_noob Posted January 26, 2014 Share Posted January 26, 2014 I am actually still beating chapter 2 to death, but started looking ahead to see what the future chapters are all about. In chapter 10, page 349 I find this script 10.5 (aka 11.5 in my book) that uses a switch to check which format was passed through the POST. Starting on line 34 of the code, Larry uses an if/else conditional to select the format based on what the requester submitted. My question is this, why would we not use another switch statement instead of the if/else? The switch looks cleaner and makes it easier to see at a quick glance what is going on. Not that it is confusing the way the code is written, it just isn't as clean and straight forward. I am just wondering if there is a reason to not use a switch in this specific case even though we are checking the value of the same variable throughout the if/else. In my learning, I like to see how other programmer's were thinking when a decision was made to do it "this was, or that way". Here is the code in question: <?php # Script 10.5 - service.php // This script acts as a simple Web service. // The script only reports back the data received, along with a bit of extra information. // Check for proper usage: if (isset($_POST['format'])) { // Switch the content type based upon the format: switch ($_POST['format']) { case 'csv': $type = 'text/csv'; break; case 'json': $type = 'application/json'; break; case 'xml': $type = 'text/xml'; break; default: $type = 'text/plain'; break; } // Create the response: $data = array(); $data['timestamp'] = time(); // Add back in the received data: foreach ($_POST as $k => $v) { $data[$k] = $v; } // Format the data accordingly: if ($type == 'application/json') { $output = json_encode($data); } elseif ($type == 'text/csv') { // Convert to a string: $output = ''; foreach ($data as $v) { $output .= '"' . $v . '",'; } // Chop off the final comma: $output = substr($output, 0, -1); } elseif ($type == 'text/plain') { $output = print_r($data, 1); } } else { // Incorrectly used! $type = 'text/plain'; $output = 'This service has been incorrectly used.'; } // Set the content-type header: header("Content-Type: $type"); echo $output; Link to comment Share on other sites More sharing options...
HartleySan Posted January 26, 2014 Share Posted January 26, 2014 I think the following will shed some light on this: http://stackoverflow.com/questions/4241768/switch-vs-if-statements I would recommend reading all the answers in the thread, as they all offer some valuable insight. Link to comment Share on other sites More sharing options...
another_noob Posted January 26, 2014 Author Share Posted January 26, 2014 Thanks HartleySan for the reply. I studied that thread, but can't seem to apply it to this exact situation. Maybe I am missing something. I wrote the if/else into a switch as follows: switch($type){ case 'application/json': $output = json_encode($data); break; case 'text/csv': $output = ''; foreach($data as $v){ $output .= '"' . $v . '",'; } $output = substr($output, 0, -1); break; case 'text/plain': $output = print_r($data, 1); break; default: $type = 'text/plain'; $output = 'This service has been incorrectly used.'; } I haven't tested the code, this is just for example. I can't find a reason to not write it this way as opposed to an if/else conditional. Surely speed can't be the issue or the switch statement above it in the original code would be an if/else. There are as many cases in my switch as there is in the original code's switch, so the number of conditions sure can't be a reason to use if/else. I am thinking that the switch in the original code could of course be easily rewritten as an if/else and function just fine. This is just an OCD thing for me now. I have to know why (in this exact case) Larry chose to do it the way he did. There must be a very specific reason. Assuming my rewrite of the if/else is correct, which way would you be inclined to write this code? Maybe I should go back to chapter 2 and shut up...lol. Link to comment Share on other sites More sharing options...
HartleySan Posted January 27, 2014 Share Posted January 27, 2014 Well, we can only hope that Larry personally chimes in if you want to know why he wrote what he did. As for the thread I linked to and my own understanding of if/else vs. switch, I feel like they solve two different things. Switch statements are used when you have a set number of simple values that you are testing against (be them strings or numbers), and if/else statements are used for handling more complex logic (e.g., $x > 3 && $x * 21 < 5,653). That make sense? Link to comment Share on other sites More sharing options...
Larry Posted January 27, 2014 Share Posted January 27, 2014 I don't like to put logic within the cases of a switch (e.g., the foreach). Link to comment Share on other sites More sharing options...
another_noob Posted January 28, 2014 Author Share Posted January 28, 2014 Ah ha, that makes sense. I knew there had to be a very specific reason. Thanks Larry. Thanks HartleySan for all your input. I appreciate all your time and input you lend to this forum. Link to comment Share on other sites More sharing options...
Larry Posted January 29, 2014 Share Posted January 29, 2014 Yes, my continued thanks to HartleySan for is time and input, too. Link to comment Share on other sites More sharing options...
Recommended Posts