Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Whats the best function to use in PHP for a massive comparison?

Update:

The scenario is I have a user register with the site and they state their country. I then want to assign a flag to them based on their country of choice. IE

if($country == "poland")

{

//polands national flags path

}

etc etc

7 Answers

Relevance
  • 4 years ago

    Search on Google

  • 4 years ago

    5900 Wilshire Blvd

  • 4 years ago

    Hiiiiiiiiiii

  • 4 years ago

    I store the country as the two letter international code in the user database - in my case using an html select entry that has the code as "value" against each country name, but you could just use a static array to convert names<>codes.

    Then use that two letter code as part of the flag filename.

    I also have two sets in different sizes, a small one for in the site header next to the persons username and a large one for the user info page.

    Using codes rather than literal names also means it's easier to make the site multilingual in future.

    Standard codes:

    http://www.nationsonline.org/oneworld/country_code...

  • How do you think about the answers? You can sign in to vote the answer.
  • 4 years ago

    The fastest (and easiest) way is to use an array. PHP arrays can use any object as an index, not just the usual "integer from 0 to n-1". Arrays with arbitrary index values are called "associative arrays" in PHP. (In JS, too, but some languages use terms like "map" or "dictionary" to mean the same thing.)

    To declare and initialize an "associative" PHP array, use the array() syntax with key=>value pairs:

    $flags_by_country = array( "poland"=>"eur/poland.png", "france"=>"eur/france.png", "japan"=>"asia/japan.png", ... etc. );

    That is evaluated just once. I've left off the "http://"/ at the front of the URL to save space, and you can also omit the .png/.gif/.jpg and the leading directory prefix if they are all the same file type in the same place.

    With that, you could then use:

    if (in_array(flags_by_country, country)) { /* if there's a flag icon defined */

    $flag_url = "http://{$flags_by_country[country]}";

    } else {

    $flag_url = "http://images/no_flag.png%22;

    }

    echo "<img src='{$flag_url}' alt='Flag of $country'>";

    ...or something like that. My PHP is a bit rusty. Only one if-statement and one array lookup is needed, The if statement keeps you from generating errors when the $country value is not a key in the array.

    For even more flexibility, you could have start-up code load the key and value pairs from a database table or text file. That might allow non-programmers to expand the set of country flags without having to touch any PHP source code.

  • ?
    Lv 4
    4 years ago

    You can store the countries in an array and then use a foreach loop.

    $country = 'england';

    $countries = array('france', 'poland', 'england', 'italy');

    foreach($countries as $country2)

    {

    if($country == $country2)

    {

    // do whatever

    }

    }

  • Chris
    Lv 7
    4 years ago

    The best way to do this is this:

    $fn = "flags/" . $country . ".png";

    if (!file_exists($fn)) $fn = "flags/kekistan.png";

    echo "<img src='$fn'>";

    You're obviously not going to use 150 ifs or switch cases or whatever else you had in mind.

    The key here is to understand that a folder full of PNGs already constitutes a list you can use in your program; you don't have to create an identical copy inside your code.

Still have questions? Get your answers by asking now.