Problem with "switch"

Discussion in 'PHP' started by WPPJames, Jun 26, 2010.

  1. #1
    I'm sure I'm missing something really simple, but the code below causes an error:

    $cntry_lvl = $row["co_level"];
    
    if $v_uid == 'ZUXW-FYJS-XJBW-TEQR' {
    	switch ($cntry_lvl) {
    	case 1:
    	  $minadrate = .02;
    	  break;
    	case 2:
    	  $minadrate = .015;
    	  break;
    	case 3:
    	  $minadrate = .01;
    	  break;
    	default:
    	 $minadrate = .004;
    	}
    }
    Code (markup):
    Any idea why?

    Thanks,
    James
     
    WPPJames, Jun 26, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    off the top of my head I'd be concerned that $cntry_lvl isn't an integer and so the "match" isn't working right. What line do you get the error with and what does the error say?
    $cntry_lvl = $row['co_level'];
    
    if $v_uid == 'ZUXW-FYJS-XJBW-TEQR' {
    	switch ($cntry_lvl) {
    	case '1':
    	  $minadrate = .02;
    	  break;
    	case '2':
    	  $minadrate = .015;
    	  break;
    	case '3':
    	  $minadrate = .01;
    	  break;
    	default:
    	 $minadrate = .004;
    	}
    }
    PHP:
     
    sarahk, Jun 26, 2010 IP
  3. WPPJames

    WPPJames Well-Known Member

    Messages:
    281
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    153
    #3
    Thanks Sarah!

    After a change to:

    $ctlvl = (integer) $cntry_lvl;
    if $v_uid == 'ZUXW-FYJS-XJBW-TEQR' {
    switch ($ctlvl) {

    ...all works great.

    Much appreciated,
    James
     
    WPPJames, Jun 26, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Also you might want to add parenthesis around your if statement, to convert a string to integer use the (int) typecast or the intval() function.
     
    danx10, Jun 27, 2010 IP