Please Cleanup if statements?

Discussion in 'PHP' started by mokimofiki, Sep 15, 2010.

  1. #1
    I am just wondering what the best way to cleanup this code is to make it smaller. Right now its an awful lot of code for what is being done.

    if($num6 == 1){$num6 = "a";}
    if($num6 == 2){$num6 = "b";}
    if($num6 == 3){$num6 = "c";}
    if($num6 == 4){$num6 = "d";}
    if($num6 == 5){$num6 = "e";}
    if($num6 == 6){$num6 = "f";}
    if($num6 == 7){$num6 = "g";}
    if($num6 == 8){$num6 = "h";}
    if($num6 == 9){$num6 = "i";}
    if($num6 == 10){$num6 = "j";}
    if($num6 == 11){$num6 = "k";}
    if($num6 == 12){$num6 = "l";}
    if($num6 == 13){$num6 = "m";}
    if($num6 == 14){$num6 = "n";}
    if($num6 == 15){$num6 = "o";}
    if($num6 == 16){$num6 = "p";}
    if($num6 == 17){$num6 = "q";}
    if($num6 == 18){$num6 = "r";}
    if($num6 == 19){$num6 = "s";}
    if($num6 == 20){$num6 = "t";}
    if($num6 == 21){$num6 = "u";}
    if($num6 == 22){$num6 = "v";}
    if($num6 == 23){$num6 = "w";}
    if($num6 == 24){$num6 = "x";}
    if($num6 == 25){$num6 = "y";}
    if($num6 == 26){$num6 = "z";}
    Code (markup):
     
    mokimofiki, Sep 15, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    $a2z = range('a', 'z');
    
    $num6 = $a2z[$num6];
    
    PHP:
     
    Last edited: Sep 15, 2010
    danx10, Sep 15, 2010 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Thank you for the response. As I was waiting it hit me to just use the code below:

    $letters = 'abcdefghijklmnopqrstuvwxyz';
    $num6 = "$letters[$num6]";
     
    mokimofiki, Sep 15, 2010 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    This will work too:

    $num6=chr($num6+96);
    PHP:
     
    plog, Sep 15, 2010 IP