You are here: Home / Topics / How to Populate Dropdown List using Array in PHP

How to Populate Dropdown List using Array in PHP

Filed under: PHP on 2023-07-02 21:20:14

In this tutorial, we are going to see how to populate dropdown list using array in PHP. You can simply use the foreach loop to create a <select> list or any drop-down menu that forms the values of an array.
 

How to Populate Dropdown List using Array in PHP
 

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
   <title>Populate Dropdown List using Array</title>
 </head>
 <body>
   <select>
     <option selected="selected">Select a value</option>
     <?php
     $languages = array("PHP", "Java", "Ada", "HTML", "CSS");
     
     // Iterating through the languages array
     foreach($languages as $value){
     ?>
     <option value="<?php echo strtolower($value); ?>"><?php echo $value; ?></option>
     <?php
     }
     ?>
   </select>
 </body>
</html>


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.