Everyday, I see lots of people using code looking something like this:
<?php $rand = mt_rand(1,10); if ($rand == 1) {echo 'What ever you want to do if $rand == 1';} elseif ($rand == 2) {echo 'What ever you want to do if $rand == 2';} elseif ($rand == 3) {echo 'What ever you want to do if $rand == 3';} elseif ($rand == 4) {echo 'What ever you want to do if $rand == 4';} elseif ($rand == 5) {echo 'What ever you want to do if $rand == 5';} elseif ($rand == 6) {echo 'What ever you want to do if $rand == 6';} elseif ($rand == 7) {echo 'What ever you want to do if $rand == 7';} elseif ($rand == 8) {echo 'What ever you want to do if $rand == 8';} elseif ($rand == 9) {echo 'What ever you want to do if $rand == 9';} elseif ($rand == 10) {echo 'What ever you want to do if $rand == 10';} ?>
When, surely, it is easier to use the switch command, which will require less code (although more whitespace is common) and will be more readable, it'd look something like this:
<?php $rand = mt_rand(1,10); switch ($rand) { case 1:echo 'What ever you want to do if $rand == 1';break; case 2:echo 'What ever you want to do if $rand == 2';break; case 3:echo 'What ever you want to do if $rand == 3';break; case 4:echo 'What ever you want to do if $rand == 4';break; case 5:echo 'What ever you want to do if $rand == 5';break; case 6:echo 'What ever you want to do if $rand == 6';break; case 7:echo 'What ever you want to do if $rand == 7';break; case 8:echo 'What ever you want to do if $rand == 8';break; case 9:echo 'What ever you want to do if $rand == 9';break; case 10:echo 'What ever you want to do if $rand == 10';break; } ?>
Of course, use of switch is not limited to number generated by the mt_rand command, it's just what I've chosen to use in my example here, partly because I've been using mt_rand quite a lot recently...
Relevent PHP Manual Pages:
Comments
Ashwin says…
February 6, 2005 11:29:39+00:00
In this particular case I don't think it really matters if you use the if-else construct or the switch-case construct as both are going to be just as long and cumbersome to deal with.
The switch-case construct is indeed very useful, but I think most people prefer to use if-else due to the ability of using anything an expression to test.
Regards
kenny says…
February 6, 2005 12:04:04+00:00
holy shit kid. you're only 12 ?? :-O
I feel old for saying this.. but when I was 12 I was still playing outside and riding my bike and having fun! :P
Not sitting inside and learning how to program PHP and keep a blog.
Although I must say it's awesome there are kids like you who learn and catch on so quickly. Keep it up. Good job on the site too. Nice, yet very basic.
Geoffrey Sneddon says…
February 7, 2005 02:23:51+00:00
Ashwin: I know this really isn't the best example, but it was the best short one I could think of at the time, in the case I showed above, I would use the switch command because it looks cleaner :D
kenny: Don't worry, I'm almost 13 :D As for the comment on my blog, thanks...
Aaron says…
February 9, 2005 07:33:28+00:00
When using random integers, it's very unlikely you'll be executing a completely different set of commands/procedures depending on the number generated. A more realistic example would be:
Switches are good for testing the value of a single variable, but getting around huge if/else combos sometimes isn't always possible.
Aaron says…
February 9, 2005 07:33:56+00:00
Sweet. Your comment thing stripped my entire example. Might want to fix that...
Geoffrey Sneddon says…
February 9, 2005 08:33:29+00:00
Hmm... Well, it's completely gone... Another reason not to use development versions of WordPress... Email it to me...
Aaron says…
February 9, 2005 10:37:26+00:00
Here's a stripped-down version:
You could fix WordPress to not strip PHP. Just need to find out where it does the 'cleaning' of posts. Is WP Perl or PHP?
J. Livingston says…
February 9, 2005 13:42:47+00:00
Be careful about removing the code stripping functions. You could create a big security hole in your site. You could set something up that would allow a <code> tag. In fact, it may already be available in commenting, but I'm not sure. Let's see:
Geoffrey Sneddon says…
February 10, 2005 04:09:13+00:00
Aaron: If you put the code in <code> or <pre> tags it should be allowed... and it's WP PHP
Mithoric says…
March 2, 2005 05:09:04+00:00
The stack or cascading elif control structure is one of the best features of the C language and it's counterparts. It allows for testing of multiple, and more complex conditions whereas switch does not. You might also notice that using switch you inevitably have to resort to some spaghetti code to stop overflow into another condition.
Mithoric says…
March 2, 2005 05:14:05+00:00
Forgot to add:
And of course spaghetti code is bad coding practice, and any programmer who delves too deeply into this practice will eventually shoot himself/herself in the foot.
Did you not stop to think before posting that if it was possible to do both of these then there must be a decent reason? C is hardly a perfected language I will admit, and as PHP is a C script it has the same characteristics (but then there are benefits of variable and memory management being taken out of the hands of the programmer, but for some people this is also a curse. I for one would rather manage my variable typing and memory management myself, it's just not possible to have an optimal PHP application otherwise) which they could have as easily taken out, but they didn't.
Geoffrey Sneddon says…
March 3, 2005 03:13:59+00:00
I'm not saying that the switch command is always the rights one to use, as both have their uses, I am well aware that the example above really isn't the best example... IMO, I prefer to use switch, as IMO, there is not too much spaghetti code...
jon says…
May 9, 2005 23:11:13+01:00
let the wars begin :)
variable typing i understand, because for one it allows you to program a little more defensively (i.e. at least in a compiled language see the error of your ways *before* running the program)
but memory management? will you next say you'd rather hand compile your code?
of course we're probably of different opinions on the ultimate usefulness of software--or perhaps different at what we enjoy doing.
i for one don't enjoy writing software to help write software, so i don't want to waste my time on memory management.
i welcome software that will allow me to be creative enough to get a human task done (while retaining enough of a knowledge of memory management to help out when faced with a problem).
the debate will go on and on, and in the end it's not even about what's "right", it's about different personalities of different programmers.
jon says…
May 9, 2005 23:11:58+01:00
oh yeah props to a 13 year old cat who has a blog as nice as this. i still know 30 year olders who can't get it right. :)
Geoffrey Sneddon says…
May 10, 2005 22:29:08+01:00
Personally, I prefer to not have to do memory management, but there again, I'm just lazy :)
As for me being a cat, Purr... Purr... :D