1
$user_ip = getenv('REMOTE_ADDR');

$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));

$city = $geo["geoplugin_city"];

$region = $geo["geoplugin_regionName"];

$country = $geo["geoplugin_countryName"];

$continent = $geo["geoplugin_continentCode"];

$continent1 = $continent;

echo "IP: " .$user_ip. "<br>";

echo "City: " .$city. "<br>";

echo "Region: " .$region. "<br>";

echo "Country: " .$country. "<br>";

echo "Continent: " .$continent1. "<br>";

This code is for getting the IP and location details.

Here, my problem is, I am getting continents as AS, EU, NA, SA

I need to change AS to Asia, EU to Europe, NA to North America and SA to South America. How to use an if else condition for this?

4
  • 4
    Of course it's not working, there is no if/else statement in your code... Commented Jul 28, 2015 at 5:17
  • you need to use switch case for this php.net/manual/en/control-structures.switch.php Commented Jul 28, 2015 at 5:17
  • 3
    Don't use if/else; use a lookup array (and an if just in case if your array is not exhaustive). Commented Jul 28, 2015 at 5:17
  • Bv202 i not that it wont work with that above code, did u read my question? i asked how to write.. Commented Jul 28, 2015 at 5:18

5 Answers 5

2

Try this -

switch ($continent1) {
    case "AS":
        $continent1 = 'Asia';
        break;
    case "EU":
        $continent1 = 'Europe';
        break;
    case "NA":
        $continent1 = 'North America';
        break;
    default:
        $continent1 = '';
}
Sign up to request clarification or add additional context in comments.

Comments

2

failing having this in a DB, you could try this:

$arr_continent = array('AS'=>'Asia','EU'=>'Europe','NA'=>'North America','SA'=>'South America');
echo "Continent: " .$arr_continent[$continent1]. "<br />";

it's a lot neater than a switch statement! ;)

Comments

1
switch ($continent)
{
    case "AS":
        $continent1 = 'Asia';
        break;
    case "EU":
        $continent1 = 'Europe';
        break;
    case "NA":
        $continent1 = 'North America';
        break;
    // rest of codes
}

Php Switch Statement

Comments

1

You can create a function without any use of if/else or switch statement

<?php 
   function get_continent($continent_name){
      $arr = array(
        "AS" => 'Asia',
        "EU" => 'Europe',
        "NA" => 'North America',
        "SA" => 'South America'
        // and define more keys and value pair of continent short and full name
        );

      return ($arr[$continent_name]!="") ? $arr[$continent_name] : "N/A" ;
   }

   echo get_continent("EU");

   // output - Europe
   ?>

This code will also increase reuse-ability if you need similar task to be performed again and again

Comments

0

You can use the switch method on your $continent1 variable to replace the values with what you want

switch($continent1)
{
    case 'AS':
       echo "Continent: Asia";
      break;
    case 'EU':
       echo "Continent: Europe";
       break;
    case 'NA':
       echo "Continent: North America";
       break;
    case 'SA':
       echo "South America";
       break;
    default:
       echo "nothing matched"
      break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.