Jump to content

Archived

This topic is now archived and is closed to further replies.

babskool

Simple Function Help

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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
[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]

Share this post


Link to post
Share on other sites
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]<?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;
}?>[/code]

Ken

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.