substr_count() string Function in PHP 8.2, PHP 8.3 & PHP 8.4

The `substr_count()` function in PHP counts the number of times a substring occurs in a given string. It's case-sensitive and doesn't count overlapping substrings.PHP 8.PHP 8.1,PHP 8.2,PHP 8.3,and PHP 8.4.
Syntax<?phpsubstr_count(string $haystack, string $needle, int $offset = 0, ?int $length = null): int?>
Parameters`$haystack`: The string to search in`$needle`: The substring to search for`$offset`: Optional starting position (default 0)`$length`: Optional maximum length after offset to search
Basic Example<?php$text = "Hello world, hello PHP, hello developers";$count = substr_count($text, "hello");echo $count; // Output: 2 (case-sensitive, so "Hello" is not counted)?>
Case-sensitive Example<?php$text = "PHP is fun. php is powerful. PHP is widely used.";$count = substr_count($text, "PHP");echo $count; // Output: 2 ("php" in lowercase is not counted)?>
With Offset<?php$text = "abcabcabc";$count = substr_count($text, "abc", 3); // Start searching from position 3echo $count; // Output: 2?>
Counting Overlapping Substrings<?php$text = "ababababa";$count = substr_count($text, "aba");echo $count; // Output: 2 (doesn't count overlapping occurrences)?>
Counting Empty String<?php$text = "Hello";$count = substr_count($text, "");echo $count; // Output: 6 (empty string between each character + start and end)?>
Note: In PHP 8.2, `substr_count()` works the same way as in previous versions, but always make sure to check the PHP manual for any updates or changes in behavior.