Is there a more consolidated way to code this?

Discussion in 'PHP' started by mokimofiki, May 19, 2009.

  1. #1
    Is there a more consolidated way to code this other than doing this 600 times lol?

    if ($row['experience'] >= 0 <= 19) {
        $level = 1;
    } else {
    
    if ($row['experience'] >= 20 <= 39) {
        $level = 2;
    } else {
    
    if ($row['experience'] >= 40 <= 59) {
        $level = 3;
    } else {
    
    } } }
    Code (markup):
     
    mokimofiki, May 19, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    
    if ($row['experience'] >= 0 <= 19) {
    
        $level = 1;
        
    } else if ($row['experience'] >= 20 <= 39) {
    
        $level = 2;
        
    } else if ($row['experience'] >= 40 <= 59) {
    
        $level = 3;
        
    } else {
    
    }
    
    PHP:
    or

    
    switch($row['experience']):
    
        case ($row['experience'] >= 0 AND $row['experience'] <= 19):
        	
            $level = 1;
            
        break;
        
        case ($row['experience'] >= 20 AND $row['experience'] <= 39):
        	
            $level = 2;
            
        break;
        
        case ($row['experience'] >= 40 AND $row['experience'] <= 59):
        	
            $level = 3;
            
        break;
        
        default:
    
    endswitch;
    
    PHP:
     
    jestep, May 19, 2009 IP
    mokimofiki likes this.
  3. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #3
    He doesn't mean that I think^

    You can put everything in a Array and do a for loop.
    Not sure if this works, but try something like this;
    $a = array('0', '20', '40', '60');
    	$lvl = 0;
    	for($i=0;$i<count($a);$i++){
    		if($i+1 == count($a) && $lvl==0){
    			$lvl = $i;
    		}else{
    			if($row['experience'] >= $a[$i] && $row['experience'] <= $a[$i+1]){
    				$lvl = $i;
    			}
    		}
    	}
    PHP:
     
    Sky AK47, May 19, 2009 IP
  4. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Thank you both :) both methods work very well
     
    mokimofiki, May 19, 2009 IP