在下面的代码中有一段重复的代码。这可以以另一种方式完成,以便代码不重复。无论我尝试什么,我总是以同样的方式结束。代码如下,但生产版本中的代码更多。这件事情是国家的位置。如何避免在这种情况下重复相同的代码
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.";
}
}
嗯......让我们现在看到的。 – Norman