What are some common pitfalls to avoid when using substr_count in PHP?

One common pitfall to avoid when using substr_count in PHP is not considering case sensitivity. By default, substr_count is case-sensitive, so if you are looking for a specific substring regardless of case, you need to convert both the haystack and needle to the same case before counting. This can be done using functions like strtolower or strtoupper.

// Incorrect usage without case sensitivity consideration
$haystack = "Hello World";
$needle = "hello";
$count = substr_count($haystack, $needle); // Output: 0

// Corrected code with case sensitivity handled
$haystack = "Hello World";
$needle = "hello";
$count = substr_count(strtolower($haystack), strtolower($needle)); // Output: 1