Dynamically create a drop-down list in PHP from a range of numbers * in increments *

I need to dynamically create a drop-down list of digital options in PHP, for example:

<select> <option value="120">120 cm</option> <option value="121">121 cm</option> <option value="122">122 cm</option> <option value="123">123 cm</option> <option value="etc... </select> 

I would like to indicate only the start and end numbers.

Thanks for any help.

+4
source share
2 answers
 echo "<select>"; $range = range(120,130); foreach ($range as $cm) { echo "<option value='$cm'>$cm cm</option>"; } echo "</select>"; 

The range() function can handle all the situations described in the comment.

 range(30.5, 50.5, 0.5); // 30.5, 31, 31.5, 32, etc range(30, 50, 2); // 30, 32, 34, 36, 38, 40 etc 
+14
source

also:

 echo "<select>"; for ($cm = 120; $cm <= 130; $cm++) echo "<option value='$cm'>$cm cm</option>"; echo "</select>"; 
+1
source

Source: https://habr.com/ru/post/1299164/


All Articles