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?
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>';
}
You need to use loop to output array data as text.
There are multiple loops in PHP:
For will iterate $i (can be diferent variable and different change than iteration) and will end when the condition is not true anymore.
$people = array('Joe','Jane','Mike');
for($i=0; $i<count($people); $i++) { //end when $i is larger than amount of people
echo " <li>{$people[$i]}</li>\n";
}
Very useful for unordered arrays - this loop will give you all values in the array as variable you want:
$people = array('Joe','Jane','Mike');
foreach($people as $human) { //end when $i is larger than amount of people
echo " <li>$human</li>\n";
}
Like FOR, loops while condition is met.