Home >Daily Programming >PHP Knowledge >How to set the value of drop-down box in php
Use php to get the value of the drop-down box. We can do it through PHP foreach loop. The PHP foreach loop method can create or fill the HTML 221f08282418e2996498697df914ce4e box or any drop-down menu to form an array. value. Then it is also a common functional effect in daily project development.
Now we will introduce the method of setting the value of the drop-down box in PHP with specific code examples.
Create a PHP file: demo110.php
The code example is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP设置下拉框值的示例</title> </head> <body> <form> <select> <option selected="selected">选一个</option> <?php $arr = array("PHP", "HTML", "CSS", "JavaScript"); foreach($arr as $v){ ?> <option value="<?php echo strtolower($v); ?>"><?php echo $v; ?></option> <?php } ?> </select> <input type="submit" value="提交"> </form> </body> </html>
Here we embed the PHP code directly in the HTML code. First, a selection box is created, and in the 221f08282418e2996498697df914ce4e tag of the selection box, a piece of PHP code is defined, and then a PHP array variable is declared in it. Finally, the value of each option box is output through a foreach loop.
Tips: Files containing a mixture of HTML and PHP code can only parse the PHP code if they are saved in PHP format.
The effect is as follows:
Note: The
foreach syntax structure provides a simple way to traverse the array. foreach can only be applied to arrays and objects. If you try to apply it to variables of other data types, or uninitialized variables, an error message will be issued.
foreach has two syntaxes:
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement
The first format iterates over the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop).
The second format does the same thing, except that the key name of the current unit will also be assigned to the variable $key in each loop.
This article is an introduction to the method of setting the value of the drop-down box in PHP. It is very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to set the value of drop-down box in php. For more information, please follow other related articles on the PHP Chinese website!