Php Programming Code Examples
Php > Arrays Code Examples
Function to create a separated list
Function to create a separated list
If you have ever needed to make a list (comma seperated, etc.), you probably have run into this problem. You could create a loop and add each item and a comma, for example, to a string. The only problem is you probably do not want the extra comma at the end of the string, so you could write code to remove the comma. Or you could do the following:
<!-- start of code -->
<?php
$array = array();
$i=0;
$array[$i++]="orange";
$array[$i++]="apple";
$array[$i++]="pear";
$array[$i++]="banana";
$str = implode (", ",$array);
echo "My shopping list: $str";
?>
<!-- End of code -->
This code will output the following:
My shopping list: orange, apple, pear, banana