在下面的代碼中有一段重複的代碼。這可以以另一種方式完成,以便代碼不重複。無論我嘗試什麼,我總是以同樣的方式結束。代碼如下,但生產版本中的代碼更多。這件事情是國家的位置。如何避免在這種情況下重複相同的代碼
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