Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to create a form with a country drop down list so I can just google and get all the countries but the question should I just encode it statically or generate dynamically with PHP. (Note it doesn't have to be necessary country list just any static list with lots of data)

HTML Example:

<select>
  <option value="AF">Afghanistan</option>
  <option value="AX">Åland Islands</option>
  <option value="AL">Albania</option>
  ...
</select>


or use php array i.e.

$countries = array (
    'AF' => 'Afghanistan',
    'AX' => 'Åland Islands',
    'AL' => 'Albania',
    ...
    );

and generate html

<!--Method 1-->
<select>
  <?php foreach($countries as $id => $country) echo "<option value='$id'>$country</option>\n" ?>
</select>


Also which method of generating is preferred or is more readable?

<!--Method 2-->
<select>
  <?php foreach($countries as $id => $country):?>
  <option value="<?php echo $id; ?>"><?php echo $country; ?></option>
  <?php endforeach; ?>
</select>

And finally should I escape variables with htmlspecialchars even though I know the output will be valid?

share|improve this question
add comment

2 Answers 2

This depends on your plans for maintaining the software. If it will not be maintained, it does not matter much. If it will be maintained irregularly and manually, not a big issue unless the country code list is used elsewhere, too, e.g. in a database where you store some submitted information or that contains some data per country. But other things being equal, it is better to generate a large dropdown from a table rather than hard-code it. And if the software will be regularly maintained by checking, among other things, the country list and country names against a reliable source, then a table is surely better. It is easier to generate just a table from the reference data.

Note that reliable and useful data can be difficult to find and the set of countries will change (and is subject to political debate). In your example, the second entry does not refer to a country but to an autonomous area. There are definitions of “country codes” that include many non-country entries for various purposes.

share|improve this answer
    
Any comments on two other "subquestions" or perhaps it should be posted as brand new questions? –  Templar Jun 19 at 13:55
add comment

Well, it is unlikely for the countries to change much, and even if they do, you might as well change it in HTML rather than in PHP. It really doesn't matter much, there's no point in abstracting these.

I'd go with making this static HTML, and save some performance (although probably negligible).


You would want to use PHP however, if you need extra functionality like "Apply selected on the country the user is GeoIP detected from". That is best handled with PHP.

share|improve this answer
    
I'll probably want to remember selected in case form fails validation. Also any comments on two other "subquestions" or perhaps it should be posted as brand new questions? –  Templar Jun 19 at 13:56
3  
In the specific case of country lists they DO change regularly. Just this year we got South Sudan. The standard is ISO 3166 iso.org/iso/country_codes.htm . For fixed lists based on a known and maintained standard a developer really should try to use a library instead of maintaining his own list. This relieves him of the pressure of maintaining his own list in parallel and generally helps code readability. –  Freiheit Jun 19 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.