phphtmlarraysfor-loop

How to loop through an array and print each item into an <li> tag using PHP?


I'm trying to iterate through the array using php. I want the items to be stored in an <li> tag. I'm having trouble getting the names to print out. I can get a count of the array items printed, however not the names.

    <ul>
    <?php 
        $names = array('Mike', 'Chris', 'Jane', 'Bob');
        for($i=0; $i < count($names); $i++) {
            echo "<li>" . $i . "</li>";
        }
    ?>
</ul>

Solution

  • <ul>
    <?php 
        $names = array('Mike', 'Chris', 'Jane', 'Bob');
        foreach($names as $name) {
            echo "<li>" . $name . "</li>";
        }
    ?>
    </ul>