Simie
Coding Admin
Why are you all still here?
Posts: 721
|
Post by Simie on Nov 3, 2006 16:07:39 GMT -5
"Switch" is a useful method of making your scripts more organised and smaller. I'll start with some examples. If/Elseif/Else: if($this == 'that'){ // Do this } elseif($this == 'there'){ // Do this } else { // Do this }
Switching: switch($this){ case 'that': // Do this break; case 'there': // Do This break; default: // Do this break; }
As you can see, the switch() method looks cleaner, and easier to edit. I'll explain it a bit better, $letter = 'z';
switch($letter){ case 'a': // If the letter is "A": print 'The letter is A'; break; // Stop it running on... case 'b': // If the letter is "B": print 'The letter is B'; break; // Stop it running on default: // Not A or B? Then do this: print 'The letter is neither A nor B.'; break; // Stop it running on }
The PHP Script checks each case until if finds a match, if it doesn't find a match then it won't do anything unless, it finds a default: case, in which case it would execute that. You must put "break;" after every code block, or it will think that the code after is still the it has to execute. I think I've explained that well enough, if you need more detail post
|
|
Matt
Graphics Admin
Great admin or greatest admin?
Posts: 4,891
|
Post by Matt on Nov 3, 2006 16:12:07 GMT -5
Nice job Maybe when I decide to learn some coding language I'll use it
|
|
Simie
Coding Admin
Why are you all still here?
Posts: 721
|
Post by Simie on Nov 3, 2006 16:12:40 GMT -5
Stop pretending, I know its crap
|
|
Matt
Graphics Admin
Great admin or greatest admin?
Posts: 4,891
|
Post by Matt on Nov 3, 2006 16:16:58 GMT -5
Even if it is, I wouldn't know
|
|
Samuel
Former Coding Moderator
:D
Posts: 154
|
Post by Samuel on Nov 3, 2006 17:17:23 GMT -5
It's also nearly EXACTLY the same in JavaScript switch(expression){ case 1: // whatever here break; case 2: //whatever here break; }
Same words could be sued for the tutorial .
|
|
No, it's Cymbals.
Banned
You're not as clever as you think you are.
rofl
Posts: 2,206
|
Post by No, it's Cymbals. on May 8, 2007 17:30:11 GMT -5
It's also nearly EXACTLY the same in JavaScript switch(expression){ case 1: // whatever here break; case 2: //whatever here break; }
Same words could be sued for the tutorial . Why sue the tutorial. He hasent hurt anybody
|
|