Example 1 — Count a Letter
How many times does "a" appear in "banana"?
<?php
$word = "banana";
echo substr_count($word, "a"), PHP_EOL; How It Works
- Choose a needle. We look for
"a". - Scan the string. PHP finds it at positions 1, 3, and 5.
- Count matches. Three matches are found.
- Print. The function returns
3.

