php if else条件不起作用

时间:2015-07-28 05:15:13

标签: php html

$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>";

此代码用于获取IP和位置详细信息。

在这里,我的问题是,我正在成为AS,EU,NA,SA等大陆

我需要将AS更改为亚洲,欧盟更改为欧洲,NA更改为北美,SA更改为南美洲。如何使用if else条件?

5 个答案:

答案 0 :(得分:2)

试试这个 -

switch ($continent1) {
    case "AS":
        $continent1 = 'Asia';
        break;
    case "EU":
        $continent1 = 'Europe';
        break;
    case "NA":
        $continent1 = 'North America';
        break;
    default:
        $continent1 = '';
}

答案 1 :(得分:2)

在数据库中没有这个,你可以试试这个:

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

它比switch语句更整洁! ;)

答案 2 :(得分: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

答案 3 :(得分:1)

您可以创建一个功能而无需使用 if / else 切换语句

<?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
   ?>

如果您需要一次又一次地执行类似任务,此代码还将提高重用能力

答案 4 :(得分:0)

您可以使用$continent1变量上的switch方法将值替换为您想要的值

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;
}