babskool Posted October 12, 2006 Share Posted October 12, 2006 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 :) Link to comment https://forums.phpfreaks.com/topic/23770-simple-function-help/ Share on other sites More sharing options...
wildteen88 Posted October 12, 2006 Share Posted October 12, 2006 From looking at the code the variable $step3_country1 is set outside of the country functionvariables 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[code]function country($step3_country1) { // your code here for the function}$country_name = country($step3_country1);echo $country_name;[/code]Or define the $step3_country1 variable as global within your function:[code]function country() { global $step3_country1; // your code here for the function}[/code] Link to comment https://forums.phpfreaks.com/topic/23770-simple-function-help/#findComment-107968 Share on other sites More sharing options...
kenrbnsn Posted October 12, 2006 Share Posted October 12, 2006 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.[code]<?phpfunction 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;}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/23770-simple-function-help/#findComment-107971 Share on other sites More sharing options...
babskool Posted October 12, 2006 Author Share Posted October 12, 2006 Thanks Guys.. All of it worked.... :) Link to comment https://forums.phpfreaks.com/topic/23770-simple-function-help/#findComment-107972 Share on other sites More sharing options...
babskool Posted October 12, 2006 Author Share Posted October 12, 2006 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 :) Link to comment https://forums.phpfreaks.com/topic/23770-simple-function-help/#findComment-107980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.