PHP: Creating a dropdown list using PHP

This entry is part 50 of 54 in the series PHP Tutorial

Now instead of getting the selected value from a dropdown list, let’s use PHP to create a dropdown list.

To create a list, we need to understand a bit more about array, especially associative array.

$arr = array('one'=>'ONE', 'two'=>'TWO', 'three'=>'THREE');

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />";

Please try to run the code here.

The foreach statement allows us to print out the key value pair.

This key value combination is what we need for creating a dropdown list.

<select name='dropdown'>


    $car = array('bmw' => 'BMW', 'renault' => 'Renault', 'toyota' => 'Toyota');
    $length = count($car);

    foreach ($car as $key => $value) {
        echo "<option value=\'$key\'>$value</option>";


Please try to run the code here.

Series Navigation<< PHP: Drop down select list
PHP: Email validation >>

Leave a comment

Leave a Reply