简体   繁体   中英

How to avoid repeating the same code in this case

In the code below there's a chunck of code that repeats itself. Can this be done in another way so that the code does not repeat. No matter what I try, I keep ending with the same thing. The code is below, but it's a lot more in the production version. This thing does country location.

if ($GL)
{
 echo 'Managed to find your location';
}else{
 echo "Could not identify GL. Please select from the list below.";
}

This is the entire thing (stripped down).

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

}

There are a couple simple solutions you could do. Such as:

1) Put it in a function:

function validGL($GL)
{
    if ($GL)
    {
        echo 'Managed to find your location.Carrying on.';
    }
    else
    {
        echo "Could not identify GL. Please select from the list below.";
    }
}

2) Store a boolean to determine if a valid location was found:

$GL = false; //GL is detected using ip to location, and returns boolean
$location = 'UK';//Read from a cookie.

$locationFound = false;

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 />';

        $locationFound = true;
    }
    else
    {
        echo 'Did not find a country in the array. Looking for GL';
    }
}
else
{
    echo 'Did not find a location cookie<br />';
}

if (!$locationFound)
{
    if ($GL)
    {
        $GL_msg = 'Managed to find your location. Carrying on';
    }
    else
    {
        $GL_msg = "Could not identify GL. Please select from the list below.";
    }
}

You can rephrase it like this:

  1. If location is passed and found within valid list of countries, use that.

  2. If not, if GL is found, use that.

  3. Show the list if all else fails.

In code:

if (isset($location) && array_key_exists($location,$countries)) {
    echo 'We found a country in the array. Carrying on<br />';
} elseif ($GL) {
    echo 'Managed to find your location. Carrying on';
} else {
    echo "Could not identify GL. Please select from the list below.";
}

You can make it a function and just call that function with the GL variable. This way you dont have to repeat the same if over and over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM