Saturday, January 5, 2008

ListCount

The ListCount feature is a very basic function. The ListCount function merely reduces 2 lines of code into one and standardizes the way you count list elements. The ListCount function comes in hand when reading through a CSV file or saving data in a list. This function should operate in PHP in the same manner as ColdFusion!

The Function:

<?php

function ListCount($list, $delimiter="")
{
if($delimiter == "")
{
$delimiter = ",";
}
$a = explode($delimiter,$list);
return count($a);
}


?>

Usage:
Using the ListCount function is simple.

<?php

$list = "a,b,c,d";
echo ListCount($list);

?>

The Result: 4

Or, you can declare a delimiter:

<?php
$list = "CATDOGMOUSEFOXHORSE";
echo ListCount($list);
?>
The Result: 5

1 comment:

Classifieds php said...

I just stumbled upon your informative blog and wanted to say that I have really enjoyed reading your blog posts. I will be your frequent visitor.