Norman
Norman

Reputation: 6365

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

}

Upvotes: 3

Views: 213

Answers (3)

Ja͢ck
Ja͢ck

Reputation: 173522

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

Upvotes: 1

Supericy
Supericy

Reputation: 5896

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

Upvotes: 3

RisingSun
RisingSun

Reputation: 1733

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.

Upvotes: 0

Related Questions