在下面的代码中有一大段重复的代码。这可以用另一种方式完成,这样代码就不会重复。无论我尝试什么,我都会以同样的方式结束。代码在下面,但在生产版本中要多得多。这东西做国家定位。
if ($GL)
{
echo 'Managed to find your location';
}else{
echo "Could not identify GL. Please select from the list below.";
}
这是整个事情(精简)。
$GL = false; //GL is detected using ip to location, and returns boolean
$location = 'UK';//Read from a cookie.
if(isset($location))
{
echo 'We found a cookie with your location<br />';
if(array_key_exists($location,$countries))
{
echo 'We found a country in the array. Carrying on<br />';
}else
{
echo 'Did not find a country in the array. Looking for GL';
if ($GL)
{
echo 'Managed to find your location. Carrying on';
}else{
echo "Could not identify GL. Please select from the list below.";
}
}
}
else
{
echo 'Did not find a location cookie<br />';
if ($GL)
{
echo 'Managed to find your location.Carrying on.';
}else{
echo "Could not identify GL. Please select from the list below.";
}
}