Jump to content


Photo

PHP Switch Statement


  • Please log in to reply
4 replies to this topic

#1 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 20 September 2006 - 01:02 AM

I Am getting a syntax error with this code:

switch(true){
case ($owp >= .667){
$region_points = $region_points+14;
}
case (($owp < .667) &&($owp >= .5)){
$region_points = $region_points+12;
}
case (($owp < .5) &&($owp >= .333)){
$region_points = $region_points+10;
}
case ($owp < .333){
$region_points = $region_points+8;
}
}

I got this from the php.net docs but i guess you can't do this. What is the code to do this idea?  Any help is appreciated.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 September 2006 - 01:04 AM

Your syntax error comes from trying to start integers with a decimal. Try using 0.5 instead of .5 etc etc

#3 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 20 September 2006 - 01:16 AM

Thorpe, I tried that and I am getting the same parse error at the first line of the switch statement.

#4 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 20 September 2006 - 01:22 AM

I think i needed a colon where i had brackets. My bad.

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 September 2006 - 01:26 AM

Sorry, your syntax is pretty whack here. Try...

switch(true){
   case ($owp >= 0.667) :
      $region_points = $region_points+14;
      break;
   case (($owp < 0.667) && ($owp >= 0.5)) :
      $region_points = $region_points+12;
      break;
   case (($owp < 0.5) && ($owp >= 0.333)) :
      $region_points = $region_points+10;
      break;
   case ($owp < 0.333) :
      $region_points = $region_points+8;
      break;
}

Ive never used this method with mutliple choices (ie &&) so... might not work. You really probably should really be using if else ifs here.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users