Jump to content


Photo

Simple Function Help


  • Please log in to reply
4 replies to this topic

#1 babskool

babskool
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 October 2006 - 05:12 PM

I'm trying to use a simple function command and trying to display the country name according to the number chosen but this code some how doesn't work.... It Only Shows the last option: "No Country Selected" Although i tried with all the numbers.... Please let me know whats missing on the code...


<?php function country() {
if($step3_country1 == "1"){ echo"Brazil";
}elseif($step3_country1 == "2"){ echo"China";
}elseif($step3_country1 == "3"){ echo"Costa Rica";
}elseif($step3_country1 == "4"){ echo"India";
}elseif($step3_country1 == "5"){ echo"Ghana";
}elseif($step3_country1 == "6"){ echo"Kenya";
}elseif($step3_country1 == "7"){ echo"Nepal";
}elseif($step3_country1 == "8"){ echo"Peru";
}elseif($step3_country1 == "9"){ echo"South Africa";
}elseif($step3_country1 == "10"){ echo"Tanzania";
}elseif($step3_country1 == "11"){ echo"Thailand";
}elseif($step3_country1 == "12"){ echo"Sri Lanka";
}else{ echo"No Country Selected";
}
}

$country_name = country();
echo $country_name;
?>

Thanks,
Shawn :)

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 October 2006 - 05:18 PM

From looking at the code the variable $step3_country1 is set outside of the country function

variables within in a function do no have global scope, meaning they can only be used within the function and not outside of the function.

In order for your country function to use the $step3_country1 variable, you'll either have to pass it as a parameter
function country($step3_country1) {
   // your code here for the function
}

$country_name = country($step3_country1);
echo $country_name;

Or define the $step3_country1 variable as global within your function:
function country() {
     global $step3_country1;

     // your code here for the function
}


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 October 2006 - 05:22 PM

Where is $step3_country1 coming from? You either have to pass it in via the function parameters or declare in global in the function. I would also use a switch statement instead of the if/elseif statements you currently use.

<?php
function country($step3_country1) {
    switch ($step3_country1) {
       case '1':
          $c = 'Brazil';
          break;
       case '2':
          $c = 'China';
          break;
       case '3':
          $c = 'Costa Rica';
          break;
       case '4':
          $c = 'India';
          break;
       case '5':
          $c = 'Ghana';
          break;
       case '6':
          $c = 'Kenya';
          break;
       case '7':
          $c = 'Nepal';
          break;
       case '8':
          $c = 'Peru';
          break;
       case '9':
          $c = 'South Africa';
          break;
       case '10':
          $c = 'Tanzania';
          break;
       case '11':
          $c = 'Thailand';
          break;
       case '12':
          $c = 'Sri Lanka';
          break;
       case '1':
          $c = 'Brazil';
          break;
       default:
          $c = 'No Country Selected';
    }
   echo $c;
}?>

Ken

#4 babskool

babskool
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 October 2006 - 05:26 PM

Thanks Guys.. All of it worked.... :)

#5 babskool

babskool
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 October 2006 - 05:44 PM

Hey guys i have one more question:

What if i want to display the result inside something like this: $message.="Country $country_name";



Please let me know..


Thanks,
Shawn :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users