Home >Backend Development >PHP Tutorial >How to understand the grammatical rules of this sentence?

How to understand the grammatical rules of this sentence?

WBOY
WBOYOriginal
2016-08-04 09:20:57996browse

<code>    <?php
        foreach($hobby as $v){
            echo "<input type='checkbox' name='hobby[]' value='".$v."'/>".$v;
        }
    ?></code>

This is a fragment of a check box, hobby[] is an array, what are the syntax rules for this? How to understand it? Doesn't creating an array require array? Thank you

Reply content:

<code>    <?php
        foreach($hobby as $v){
            echo "<input type='checkbox' name='hobby[]' value='".$v."'/>".$v;
        }
    ?></code>

This is a fragment of a check box, hobby[] is an array, what are the syntax rules for this? How to understand it? Doesn't creating an array require an array? Thank you

The foreach() function of PHP is used to traverse the entire array. foreach($hobby as $v) is to obtain each item (denoted as $v) in the array $hobby[] in sequence.

This is not creating an array but traversing each item in the array $hobby[] and outputting it as a checkbox value.

For example, the length of $hobby[] is 5, this code will output a total of 5 checkboxes, and the value of each checkbox corresponds to an item in the array.

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