English 中文(简体)
Cleaner way to translate index into specified string?
原标题:

Could I achieve this in a more cleaner way? Would be really appricated.

    switch($vocation) {

        case 1: $vocation = "Sorcerer";        break;
        case 2: $vocation = "Druid";           break;       
        case 3: $vocation = "Paladin";         break;           
        case 4: $vocation = "Knight";          break;           
        case 5: $vocation = "Master Sorcerer"; break;           
        case 6: $vocation = "Elder Druid";     break;           
        case 7: $vocation = "Royal Paladin";   break;           
        case 8: $vocation = "Elite Knight";    break;

    }

Thanks!

and sorry about the title couldnt figure out a better one

问题回答

You can use an array instead of the switch statement.

$vocations = array("Sorcerer", "Druid" ...);
$vocationStr = $vocations[$vocation - 1];

Or

$vocations = array(1 => "Sorcerer", 2 => "Druid" ...);
$vocationStr = $vocations[$vocation];

Personally, I am against the reuse of $vocation to refer to two types (integers and String), as they are most likely two different concepts in your program.

I would also advise creating an Enum type for these values instead, but this depends on the context.

Well, depending on what you re using this for (not really sure as there is no context) one option is to have your vocations available in a configuration file (be it XML, text file, etc) that can be read in by the system on the fly. This way, if you ever want to add an additional vocation, you can just update the text file and you don t need to rebuild and/or redeploy your source.

Either using an array as already mentioned, or, at the very least, using constatants rather than the plain integers in the switch statement. So something like:

define("SORCERER", 1);
define("DRUID", 2);
...
define("RPALADIN", 7);
define("EKNIGHT", 8);

switch($vocation) {
    case SORCERER: $vocation="Sorcerer"; break;
    case DRUID: $vocation="Druid"; break;
    ...
    case RPALADIN: $vocation="Royal Paladin"; break;
    case EKNIGHT: $vocation="Elder Knight"; break;
}

That way adding new classes and ranks between classes is a little easier.

This gives a trap for initial values of vocation not found in your list.

$vocations = array(1=>"Sorcerer", 2=>"Druid", ...);
if (array_key_exists($vocation, $vocations)) {
  $vocation = $vocations[$vocation];
} else {
  $vocation =  Not found ;
}

An array can work for sequential keys, but use an associative array (hash) otherwise.

%vocations = array("key1"=>"value1", "key2"=>"value2", ...);
$vocationVal = $vocations{$vocationKey};

EDIT: The above is a perl syntax, the php syntax is below:

$vocations = array(key1 => "value1", key2 => "value2", ...);
$vocationVal = $vocations[$vocationKey];

Another option that i haven t seen here is to keep the data in a table. It will help if the list becomes huge.

vocation_id        vocation_name
     1               sorcerer
     2                druid
    etc....

select vocation_name form vocations where vocation_id=3




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签