View Full Version : PHP - Select Case / Switch help (looking for a good example)
Tekton
08-25-2004, 05:48 AM
I spent some time searching for a good php select case / switch example, but the one I found doesn't seem to work... Maybe I can get a better example since it's about 20 or so cases total, and I don't want to do that on if/elses :) I've taken out other things inside of them for sanity.
switch ($magic) {
case 0 to $chance_for_normal:
$special = 0;
break;
case $endchance to 20: //must be set lower if normal chance is reduced.
$special = 1;
break;
case 21 to 25:
$special = 3;
break;
etc.....
etc.....
Seems to just give me errors. Am I doing it wrong?
If you have trouble with php code look for an example within vBulletin or other scripts, here is an example of a proper switch statement, you can find it in memberlist.php in vBulletin 3.0.3
switch ($sortfield)
{
case 'username':
$sqlsort = 'user.username';
break;
case 'joindate':
$sqlsort = 'user.joindate';
break;
case 'posts':
$sqlsort = 'user.posts';
break;
case 'lastvisit':
$sqlsort = 'lastvisittime';
break;
case 'reputation':
$sqlsort = iif($show['reputationcol'], 'reputationscore', 'user.username');
$secondarysortsql = ', user.username';
break;
default:
$sqlsort = 'user.username';
$sortfield = 'username';
}
Is that clear enough? :)
Tekton
08-25-2004, 06:18 AM
Well, it doesn't use "to"s, or numbers, but I suppose I can try again. I did look at the memberlist one before :)
UPDATE :
For future refrence or similar questions, it's like this...
case '0 to $chance':
or
case '0 to 5':
:)
Generaly text has to be in quotes, but numbers don't.
Natch
08-25-2004, 08:33 AM
The only way to do what you wanna do without having heaps of code is to do the following:switch ($magic) {
case 1:
case 2:
case 3:
...
case 18:
case 19:
case 20:
$special = 1;
break;
case 21:
case 22:
case 23:
case 24:
case 25:
$special = 3;
break;
default:
$special = 0;
}
The logic of it is that if if comes to case 15, and doesn't come to a break before the end of case 20 then it will apply the $special = 1 for all cases up to 20 - you will have to enter each case but not define the value for each one.
Tekton
08-26-2004, 02:45 AM
The only way to do what you wanna do without having heaps of code is to do the following:switch ($magic) {
loooong thing
}
The logic of it is that if if comes to case 15, and doesn't come to a break before the end of case 20 then it will apply the $special = 1 for all cases up to 20 - you will have to enter each case but not define the value for each one.
I updated my post with a method that worked...
case '0 to $chance':
or
case '0 to 5':
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.