// Step 1: use this string.
$test = "AAbbbcccc.";
// Step 2: call count chars.
$counts = count_chars($test);
// Step 3: print some counts that exist.
echo $counts[ord('A')], "\n";
echo $counts[ord('b')], "\n";
echo $counts[ord('c')], "\n";
// Step 4: if a value does not exist, it is empty.
echo $counts[ord('?')], "\n";
// Step 5: loop over all elements in the returned array.// Print the ones that have 1 or more count.
foreach ($counts as $key => $value) {
if ($value != 0) {
echo "Key exists: " . chr($key) . " = " . $value . "\n";
}
}2
3
4
0
Key exists: . = 1
Key exists: A = 2
Key exists: b = 3
Key exists: c = 4
Some programming tasks in PHP are done with a custom function and for-loops. But with a built-in function like count_chars, we can sometimes reduce code and make programs simpler.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.