New to php: I have a simple array:
$people = array('Joe','Jane','Mike');
How do I output this into a list?
<ul>
<li>Joe</li>
<li>Jane</li>
<li>Mike</li>
</ul>
Any help or direction would be appreciated?
You can use implode()
and print the list:
echo '<ul>';
echo '<li>' . implode( '</li><li>', $people) . '</li>';
echo '</ul>';
Note this would print an empty <li>
for an empty list - You can add a check in to make sure the array isn't empty before producing any output (which you would need for any loop so you don't print an empty <ul></ul>
).
if( count( $people) > 0) {
echo '<ul>';
echo '<li>' . implode( '</li><li>', $people) . '</li>';
echo '</ul>';
}
Try:
echo '<ul>';
foreach($people as $p){
echo '<li>'.$p.'</li>';
}
echo '</ul>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With