search
HomeDaily ProgrammingPHP KnowledgeHow 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

How to set the value of drop-down box in php

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

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:

How to set the value of drop-down box in php

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools