arcanepsyche Posted January 1, 2009 Report Posted January 1, 2009 (edited) Hello all. I'm attempting to use the switch function for the first time and have a general question about the following code, which is part of a .php page I'm using to display the results of a submitted form: switch ($_POST['page_count']) {case "1": echo "There is 1 page."; break; case "2": echo "There are 2 pages."; break; case "3": echo "There are 3 pages."; break; }; Is there a way to make the code cleaner by using some sort of "through" statement? I tried this: {case "1-5": echo "There are between 1 and 5 pages"; But that didn't work, which is what I thought would happen. Or, is there some sort of greater than or less than symbol? My users are able to choose any number between 1 and 25 on the form and I'm not too keen on copying and pasting 25 different cases on the PHP page if there's a simpler way out there somewhere. Thanks in advance for the help, and Happy New Year! Zac Edited January 1, 2009 by arcanepsyche Quote
falkencreative Posted January 1, 2009 Report Posted January 1, 2009 I'll have to do a bit of experimenting and get back to you... I don't know off the top of my head. How many cases will you have? Would it make more sense to use a if / else if statement instead? Quote
falkencreative Posted January 1, 2009 Report Posted January 1, 2009 You could do a switch for 1 through 5 like this: switch ($_POST['page_count']) { case "1": case "2": case "3": case "4": case "5": echo "There are 1-5 pages pages."; break; }; (no ";" is necessary after the switch statement, FYI) As I said above though, if you have a lot of these, it may make more sense to do a long if / else if statement instead of switch. Quote
arcanepsyche Posted January 1, 2009 Author Report Posted January 1, 2009 I started out using the if/else statements but then found info on switch and it just seemed like a simpler way of doing the same thing...unless I'm not able to figure out how to do the above. Either way, thanks for experimenting for me. Don't let it eat into your champagne drinking time tonight! Quote
arcanepsyche Posted January 1, 2009 Author Report Posted January 1, 2009 Yeah, that's a bit more simplified, thanks. I have 25 of them, so I'll have to decide which format I like better.... Good to know about the ; not being necessary. Thanks! Quote
falkencreative Posted January 1, 2009 Report Posted January 1, 2009 Actually, I found a solution for you... You'll have to adapt your code after looking at my example: $i = 4; switch(true) { case ($i > 0 && $i echo "success!"; break; default: echo "failure"; break; } ?> Quote
arcanepsyche Posted January 1, 2009 Author Report Posted January 1, 2009 Ah! That's better. I knew there had to be something like that out there. Thanks much! Quote
tpattison Posted January 1, 2009 Report Posted January 1, 2009 If all you're doing is stating the number of pages, then why not: $c = $_POST['page_count']; if ($c==1) { echo "There is 1 page."; } else { echo "There are $c pages."; } Quote
arcanepsyche Posted January 1, 2009 Author Report Posted January 1, 2009 Thanks for the response. The reason I can't use that is because there is a different response for different numbers of pages so I have to add conditions. I ended up using this, which worked fine: if ($page_count >0 && $page_count <5) { $price_per_page=25; } if ($page_count >4 && $page_count <10) { $price_per_page=20; } if ($page_count >10) { $price_per_page=15; } And then later multiplied the $price_per_page by $page_count to create $total_page_price which will give my customers the price they will pay based on how many pages they want built. Thanks for your help guys! Quote
administrator Posted January 1, 2009 Report Posted January 1, 2009 Hi, You change your thinking on this and use the 'default' statement for a catch all: switch ($_POST['page_count']) { case "1": echo "There is 1 page." break; case "2": echo "There are 2 pages." break; case "3": echo "There are 3 pages." break; default: echo "There are 4 to 5 pages ... or even more!" break; }; Actually, I think the other options presented above would work better, but I wanted to mention the 'default' statement. Stefan Quote
administrator Posted January 1, 2009 Report Posted January 1, 2009 Oh wait ... default was already mentioned. My bad. Stefan Quote
arcanepsyche Posted January 1, 2009 Author Report Posted January 1, 2009 Thanks Stefan! Repeating = better learning! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.