// 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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.